postman正在执行时,javascript获取未发布到rest api端点

postman正在执行时,javascript获取未发布到rest api端点,javascript,jquery,fetch,fetch-api,Javascript,Jquery,Fetch,Fetch Api,我有一个restapi端点,我正在使用正确发布的POSTMAN检查它。但是,当我使用JAVASCRIPT获取时,我无法发布它。下面是我的获取代码: const { inputAOI, wktForCreation } = this.state fetch('http://192.168.1.127:8080/aoi/', { method: 'POST', headers: { 'Content-Type': 'application/json'

我有一个
restapi端点
,我正在使用正确发布的
POSTMAN
检查它。但是,当我使用JAVASCRIPT获取时,我无法发布它。下面是我的获取代码:

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 })
    })
下面是请求头的图像。

在上图中可以看到,在
请求头
中,
内容类型
仍然是
文本/普通
,但我正在发送
应用程序/json
,如上图所示

检查控制台中的响应预览。

以下是正确的邮递员要求: 而不是

标题:{
“内容类型”:“应用程序/json”
}

你可以试试:

标题:新标题({
“内容类型”:“应用程序/json”
})

正如评论中所暗示的,问题在于
模式:“无cors”

内容类型被认为是一个简单的标题,不允许使用cors,只能使用以下值:

  • 应用程序/x-www-form-urlencoded
  • 多部分/表单数据
  • 文本/纯文本
见:

如果在与脚本相同的主机/端口上运行API,则应使用
模式:“同一原点”
或者将脚本所运行的主机/端口添加为API上允许的原点


有关CORS的更多信息:

API是否有一个保护程序阻止以模式发送的请求:无CORS?尝试在没有参数“”的情况下发送它。这两个参数是等效的,它们将导致相同的结果