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

Unable to fetch POST without no-cors in header(无法在标头中没有 no-cors 的情况下获取 POST)
本文介绍了无法在标头中没有 no-cors 的情况下获取 POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

提出这样的要求:

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))

使用方法 OPTIONS 代替 POST 运行请求.仅在添加模式下:'no-cors' 请求变为 POST:

request running with method OPTIONS instead POST. Only on adding mode: 'no-cors' request become 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))

但响应不正常(即使网络响应状态为 200):{type: "opaque", url: "", status: 0, ok: false, statusText: ""...}我想是因为

but response not ok than (even if network response status is 200): {type: "opaque", url: "", status: 0, ok: false, statusText: ""…} I suppose it because

Content-Type 标头的唯一允许值是:application/x-www-form-urlencoded multipart/form-data text/plain

The only allowed values for the Content-Type header are: application/x-www-form-urlencoded multipart/form-data text/plain

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

有什么方法可以通过 fetch 实现 POST json 数据吗?

Is any way bring to live POST json data with fetch?

推荐答案

您发送的自定义 Content-Type 标头会导致您的请求被预检,这意味着一个包含一些元数据的 OPTIONS 请求关于即将发送的 POST 请求,将在实际 POST 请求之前发送.

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.

您的服务器需要准备好处理此 OPTIONS 请求.您尚未指定服务器的写入内容,但例如使用 express,您可以注册一个拦截所有OPTIONS"请求的中间件,设置 Access-Control-Allow-Origin: *Access-Control-Allow-Headers: Content-Type 标头,并以 200 响应.

Your server needs to be prepared to deal with this OPTIONS request. 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.

如果您可以使用 'Content-Type': 'text/plain' 标头发出请求,那将解决您的问题.或者,您可以使用完全绕过 XHR 的东西,例如 JSONP.

If it is possible for you to make the request using a 'Content-Type': 'text/plain' header, that would solve your problem. Alternatively you could use something that bypasses XHR entirely, like JSONP.

这篇关于无法在标头中没有 no-cors 的情况下获取 POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

SCRIPT5: Access is denied in IE9 on xmlhttprequest(SCRIPT5:在 IE9 中对 xmlhttprequest 的访问被拒绝)
XMLHttpRequest module not defined/found(XMLHttpRequest 模块未定义/未找到)
Show a progress bar for downloading files using XHR2/AJAX(显示使用 XHR2/AJAX 下载文件的进度条)
How can I open a JSON file in JavaScript without jQuery?(如何在没有 jQuery 的情况下在 JavaScript 中打开 JSON 文件?)
quot;Origin null is not allowed by Access-Control-Allow-Originquot; in Chrome. Why?(“Access-Control-Allow-Origin 不允许 Origin null在铬.为什么?)
How to get response url in XMLHttpRequest?(如何在 XMLHttpRequest 中获取响应 url?)