解决浏览器跨域(CORS)问题的实践 008CBA; color: white; cursor: pointer; } button:hover { backgroundcolor: 007B9E; } Send Get Request var url " function sendGetCorsRequest() { fetch(url, { method: "GET", headers:{"ContentType": "application/json",} }).then(function(res) { if (res.ok) { alert("The response is ok, get request success!"); } else { alert("The response wasn't ok, got status ", res.status); } }, function(e) { alert("Get request failed!", e); }).finally(() > { location.reload(); }); } 3.采用Chrome浏览器打开test.html文件并点击“Send Get Request”按钮,访问失败。 这个错误信息表示,请求的资源由于缺少 “AccessControlAllowOrigin”头部而被CORS(跨源资源共享)策略阻止。这通常发生在尝试从不同的域获取资源时,浏览器会检查请求是否符合CORS策略。 4.在控制台配置下方的跨域资源共享规则,接受如下请求来源以及请求方法,具体规则如下: 5.重新使用浏览器打开test.html文件,访问成功。
来自: