• <bdo id='k4kBx'></bdo><ul id='k4kBx'></ul>
    <i id='k4kBx'><tr id='k4kBx'><dt id='k4kBx'><q id='k4kBx'><span id='k4kBx'><b id='k4kBx'><form id='k4kBx'><ins id='k4kBx'></ins><ul id='k4kBx'></ul><sub id='k4kBx'></sub></form><legend id='k4kBx'></legend><bdo id='k4kBx'><pre id='k4kBx'><center id='k4kBx'></center></pre></bdo></b><th id='k4kBx'></th></span></q></dt></tr></i><div id='k4kBx'><tfoot id='k4kBx'></tfoot><dl id='k4kBx'><fieldset id='k4kBx'></fieldset></dl></div>
      <legend id='k4kBx'><style id='k4kBx'><dir id='k4kBx'><q id='k4kBx'></q></dir></style></legend>

        <small id='k4kBx'></small><noframes id='k4kBx'>

      1. <tfoot id='k4kBx'></tfoot>

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

        时间:2023-09-03

          <small id='9eRMn'></small><noframes id='9eRMn'>

          <tfoot id='9eRMn'></tfoot>
            <bdo id='9eRMn'></bdo><ul id='9eRMn'></ul>

          • <i id='9eRMn'><tr id='9eRMn'><dt id='9eRMn'><q id='9eRMn'><span id='9eRMn'><b id='9eRMn'><form id='9eRMn'><ins id='9eRMn'></ins><ul id='9eRMn'></ul><sub id='9eRMn'></sub></form><legend id='9eRMn'></legend><bdo id='9eRMn'><pre id='9eRMn'><center id='9eRMn'></center></pre></bdo></b><th id='9eRMn'></th></span></q></dt></tr></i><div id='9eRMn'><tfoot id='9eRMn'></tfoot><dl id='9eRMn'><fieldset id='9eRMn'></fieldset></dl></div>
              <legend id='9eRMn'><style id='9eRMn'><dir id='9eRMn'><q id='9eRMn'></q></dir></style></legend>
                <tbody id='9eRMn'></tbody>

                  本文介绍了无法在标头中没有 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:维基百科 API + 跨域请求 下一篇:--disable-web-security 是否可以在 Chrome 中使用?

                  相关文章

                  最新文章

                  <legend id='XzbJE'><style id='XzbJE'><dir id='XzbJE'><q id='XzbJE'></q></dir></style></legend>
                1. <small id='XzbJE'></small><noframes id='XzbJE'>

                    <i id='XzbJE'><tr id='XzbJE'><dt id='XzbJE'><q id='XzbJE'><span id='XzbJE'><b id='XzbJE'><form id='XzbJE'><ins id='XzbJE'></ins><ul id='XzbJE'></ul><sub id='XzbJE'></sub></form><legend id='XzbJE'></legend><bdo id='XzbJE'><pre id='XzbJE'><center id='XzbJE'></center></pre></bdo></b><th id='XzbJE'></th></span></q></dt></tr></i><div id='XzbJE'><tfoot id='XzbJE'></tfoot><dl id='XzbJE'><fieldset id='XzbJE'></fieldset></dl></div>

                  1. <tfoot id='XzbJE'></tfoot>

                    • <bdo id='XzbJE'></bdo><ul id='XzbJE'></ul>