【发布时间】:2018-09-07 05:17:12
【问题描述】:
我有一个rest api endpoint,并且我正在使用POSTMAN 进行检查,该POSTMAN 发布正确。但是,当我使用 JAVASCRIPT FETCH 进行操作时,我无法发布它。以下是我的获取代码:
const { inputAOI, wktForCreation } = this.state
fetch('http://192.168.1.127:8080/aoi/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ userName: 'fuseim', aoiName: inputAOI, wkt: wktForCreation }),
mode: 'no-cors'
}).then(function (response) {
if (response.ok) {
return response.json()
} else {
throw new Error('Could not reach the API: ' + response.statusText)
}
}).then(function (data) {
console.log({ data })
}).catch(function (error) {
console.log({ error })
})
从上图中可以看出,在Request Headers 中,Content-Type 仍然是text/plain,但我正在发送application/json,如上图所示。
【问题讨论】:
-
API 是否有一个守卫阻止使用 mode: no-cors 发送的请求? developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch 试试不带那个参数发送
标签: javascript jquery fetch fetch-api