简体   繁体   English

无法在标头中没有 no-cors 的情况下获取 POST

[英]Unable to fetch POST without no-cors in header

On making request like that:在提出这样的要求时:

return fetch(
            'http://localhost:8000/login',
            {   method: 'POST',
                headers: new Headers(
                   {"Content-Type": "application/json",
                    "Accept":"application/json"}
                ),

                body: JSON.stringify(
                   {'name': 'Tom', 'password': 'Soyer'}
                )
             }
           ).then( response => { console.log(response);})
            .catch(err => console.log(err))

request running with method OPTIONS instead POST.请求使用方法 OPTIONS 而不是 POST 运行。 Only on adding mode: 'no-cors' request become POST:仅在添加模式下:'no-cors' 请求变为 POST:

return fetch(
            'http://localhost:8000/login',
            {   method: 'POST',
                mode: 'no-cors',
                headers: new Headers(
                   {"Content-Type": "application/json",
                    "Accept":"application/json"}
                ),
                body: JSON.stringify(
                   {'name': 'Tom', 'password': 'Soyer'}
                )
             }
           ).then( response => { console.log(response);})
            .catch(err => console.log(err))

but response not ok than (even if network response status is 200): {type: "opaque", url: "", status: 0, ok: false, statusText: ""…} I suppose it because但响应不如(即使网络响应状态为 200):{type: "opaque", url: "", status: 0, ok: false, statusText: ""...} 我想是因为

The only allowed values for the Content-Type header are: application/x-www-form-urlencoded multipart/form-data text/plain Content-Type 标头的唯一允许值是:application/x-www-form-urlencoded multipart/form-data text/plain

described here https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS此处描述https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

Is any way bring to live POST json data with fetch?有什么方法可以通过 fetch 实时发布 JSON 数据?

The custom Content-Type header you're sending causes your request to be preflighted, which means an OPTIONS request, containing some metadata about the POST request that is about to be dispatched, will be sent before the actual POST request.您发送的自定义Content-Type标头会导致您的请求被预检,这意味着 OPTIONS 请求将在实际 POST 请求之前发送,其中包含有关即将发送的 POST 请求的一些元数据。

Your server needs to be prepared to deal with this OPTIONS request.你的服务器需要准备好处理这个 OPTIONS 请求。 You haven't specified what the server is written in, but with express for example, you can register a middleware that intercepts all 'OPTIONS' requests, sets the Access-Control-Allow-Origin: * and Access-Control-Allow-Headers: Content-Type headers, and responds with 200.您还没有指定服务器是用什么编写的,但是以 express 为例,您可以注册一个拦截所有“OPTIONS”请求的中间件,设置Access-Control-Allow-Origin: *Access-Control-Allow-Headers: Content-Type标头,并以 200 响应。

If it is possible for you to make the request using a 'Content-Type': 'text/plain' header, that would solve your problem.如果您可以使用 'Content-Type': 'text/plain' 标头发出请求,那将解决您的问题。 Alternatively you could use something that bypasses XHR entirely, like JSONP.或者,您可以使用完全绕过 XHR 的东西,例如 JSONP。

When using non-cors , all headers must be valid simple-headers .使用non-cors ,所有标头必须是有效的simple-headers The only valid values for the content-type header that qualifies as a simple-header is:符合简单标头content-type标头的唯一有效值是:

headers: [
  ['Content-Type', 'application/x-www-form-urlencoded'],
  ['Content-Type', 'multipart/form-data'],
  ['Content-Type', 'text/plain'],
]

Exceptions with contingencies:意外情况的例外:

headers: [
  ['Content-Type', 'application/csp-report'],
  ['Content-Type', 'application/expect-ct-report+json'],
  ['Content-Type', 'application/xss-auditor-report'],
  ['Content-Type', 'application/ocsp-request'],
]

If you are trying to call an api and getting stuck with this in your react app you can add a proxy to the server and the cors error will get removed如果您尝试调用 api 并在您的 React 应用程序中遇到此问题,您可以向服务器添加代理,并且 cors 错误将被删除

just add this line at the package.json只需在 package.json 中添加这一行

"proxy":"url-to-your-server",

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM