gpt4 book ai didi

javascript - CORS Origin 设置不正确?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:40:01 24 4
gpt4 key购买 nike

  1. 我有一个 JavaScript 应用程序,假设它部署在 portal.example.com 上.
  2. 其中包括 <script>加载从 assets.example.com 提供的源代码的标签.
  3. 该 JavaScript 文件向 admin.example.com 上的 API 发出 HTTP 请求

由于 CORS 预检失败,此 API 请求出错。

Failed to load http://admin.example.com/v0/user/navigation: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://portal.example.com' is therefore not allowed access.

实际OPTIONS请求如下

OPTIONS /v0/user/navigation HTTP/1.1
Host: admin.example.com
Connection: keep-alive
Access-Control-Request-Method: GET
Origin: http://portal.example.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36
Access-Control-Request-Headers: authorization,x-correlation-id,x-user-domain
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9,en-GB;q=0.8

实际OPTIONS回复如下

HTTP/1.1 200
Allow: GET
Access-Control-Allow-Headers: authorization,x-correlation-id,x-user-domain
Access-Control-Allow-Methods: GET
Access-Control-Allow-Origin: http://portal.example.com
Vary: Origin
Content-Length: 0
Date: Tue, 20 Feb 2018 12:12:19 GMT
Set-Cookie: 97d2c19dadc3933a73dce9bec0748df1=5a15895c5e0f5b526c177132cb4aa666; path=/; HttpOnly
Cache-control: private
X-RBT-SCAR: 10.127.48.7:777511903:1000

我认为问题是因为请求实际上来自 assets.example.com 提供的脚本正在提出请求。所以我应该返回 Access-Control-Allow-Origin: http://assets.example.comOPTIONS回复。但是,我听从了W3C的建议。 .

The Access-Control-Allow-Origin header indicates whether a resource can be shared based by returning the value of the Origin request header, "*", or "null" in the response.

所以我误解了 CORS,还是浏览器发送了 Origin主要执行 URL 的名称,而不是发出请求的脚本的 URL?

已更新

获取响应

HTTP/1.1 200
X-Correlation-Id: 8978b245-081a-4c4a-b4c9-73f2920ab55c
Content-Type: application/vnd.example+json
Transfer-Encoding: chunked
Date: Tue, 20 Feb 2018 13:22:39 GMT
Set-Cookie: 97d2c19dadc3933a73dce9bec0748df1=dc4e3543c3071d752959e7176c5e4d29; path=/; HttpOnly
Cache-control: private
X-RBT-SCAR: 10.127.48.7:778160108:2000

最佳答案

请求的资源上不存在“Access-Control-Allow-Origin” header 。这意味着缺少“Access-Control-Allow-Origin” header ,而不是不允许您的域。

如果你没有权限,你会看到类似的东西

“Access-Control-Allow-Origin” header 的值“http://www.example.com”不等于提供的来源。

因此,要解决您的问题,您需要配置您的 GET 响应以提供必要的 CORS header 以及 OPTIONS 响应。

在您编辑的问题中,GET 响应 header 没有为 Access-Control-* 提供任何内容,所以这就是您收到错误的原因!

关于javascript - CORS Origin 设置不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48885478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com