无法在标头中不取芯的情况下获取POST [英] Unable to fetch POST without no-cors in header

查看:66
本文介绍了无法在标头中不取芯的情况下获取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):{类型:不透明,网址:,状态:0,确定:否,状态文本: ...}
我想是因为

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 mul tipart / form-data文本/纯文本

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

是否可以通过提取将实时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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆