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

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

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

        Javascript CORS - 不存在“Access-Control-Allow-Origin"标头

        时间:2023-05-16

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

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

                    <tbody id='ZSLuu'></tbody>
                  <legend id='ZSLuu'><style id='ZSLuu'><dir id='ZSLuu'><q id='ZSLuu'></q></dir></style></legend>
                  本文介绍了Javascript CORS - 不存在“Access-Control-Allow-Origin"标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我一直在使用 CORS,遇到了以下问题.客户端抱怨没有 'Access-Control-Allow-Origin' 标头存在,而 它们存在,并且 客户端发出实际的 POST 请求 并且 收到 200.

                  I've been working with CORS and encountered the following issue. Client complains about no 'Access-Control-Allow-Origin' header is present, while they are present, and client make the actual POST request and receives 200.

                  function initializeXMLHttpRequest(url) {  //the code that initialize the xhr
                      var xhr = new XMLHttpRequest();
                      xhr.open('POST', url, true);
                      xhr.withCredentials = true;
                      xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
                  
                      //set headers
                      for (var key in headers) {
                          if (headers.hasOwnProperty(key)) {  //filter out inherited properties
                              xhr.setRequestHeader(key,headers[key]);
                          }
                      }
                  
                          return xhr;
                  }
                  

                  在 Chrome 中

                  chrome 控制台日志

                  chrome console log

                  Chrome 选项请求

                  Chrome OPTIONS request

                  Chrome POST 请求

                  Chrome POST request

                  在火狐中

                  Firefox 控制台日志

                  Firefox Console Log

                  Firefox 选项请求

                  Firefox OPTIONS request

                  Firefox POST 请求

                  Firefox POST request

                  推荐答案

                  简而言之:访问控制标头(例如 Access-Control-Allow-Origin)需要同时出现以响应 OPTIONS 和实际的POST.

                  In short: Access control headers (e.g. Access-Control-Allow-Origin) need to present in response for both OPTIONS and actual POST.

                  工作流程:

                  1. Client 使用这些 HTTP 访问标头发出 OPTIONS 请求.(例如Origin、Access-Control-Request-Method、Access-Control-Request-Headers)

                  1. Client make OPTIONS request with those HTTP access headers. (e.g. Origin, Access-Control-Request-Method, Access-Control-Request-Headers)

                  服务器响应这些访问控制标头,允许访问.(例如 Access-Control-Allow-Origin、Access-Control-Expose-Headers、Access-Control-Max-Age、Access-Control-Allow-Credentials、Access-Control-Allow-Methods、Access-Control-Allow-标题)

                  Server respond with those access control headers, allowing access. (e.g. Access-Control-Allow-Origin, Access-Control-Expose-Headers, Access-Control-Max-Age, Access-Control-Allow-Credentials, Access-Control-Allow-Methods, Access-Control-Allow-Headers)

                  客户端使用数据发出 POST 请求.

                  Client makes POST request with data.

                  服务器响应 POST.如果服务器响应中不存在 Access-Control-Allow-Origin 标头.虽然 POST 成功并在 network 选项卡中显示 200 状态码,但 xhr.status 为 0 并且会触发 xhr.onerror.浏览器会显示错误消息.

                  Server respond to POST. If Access-Control-Allow-Origin header is NOT present in the server response. Although the POST is successful and shows 200 status code in network tab, xhr.status is 0 and xhr.onerror will be triggered. And browser would show up the error message.

                  标题参考:https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

                  这篇关于Javascript CORS - 不存在“Access-Control-Allow-Origin"标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

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

                  • <legend id='cc8jG'><style id='cc8jG'><dir id='cc8jG'><q id='cc8jG'></q></dir></style></legend>
                    • <bdo id='cc8jG'></bdo><ul id='cc8jG'></ul>

                          <tbody id='cc8jG'></tbody>
                        <tfoot id='cc8jG'></tfoot>