【发布时间】:2020-07-09 03:43:09
【问题描述】:
我正在使用他们的公共 api 访问 nasa 图片,但我收到此错误:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at
[nasa api website] (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
但是当我检查他们的响应头时,'Access-Control-Allow-Origin' 存在并设置为'*',在这里你可以看到它:
回复标题:
Access-Control-Allow-Origin *
Age 0
Cache-Control max-age=0, private, must-revalidate
Content-Encoding gzip
Content-Type application/json; charset=utf-8
Date Sat, 28 Mar 2020 14:37:13 GMT
Etag W/"e26hidden..."
Referrer-Policy strict-origin-when-cross-origin
Server openresty
Strict-Transport-Security max-age=31536000; includeSubDomains
Vary Origin
Via https/1.1 api-umbrella (ApacheTrafficServer [cMsSf ]), 1.1 vegur
X-Cache MISS
X-Content-Type-Options nosniff
X-Download-Options noopen
X-Frame-Options SAMEORIGIN
X-Permitted-Cross-Domain-Policies none
X-RateLimit-Limit 1000
X-RateLimit-Remaining 999
X-Request-Id 00c8c415-37ad-474b-bfbd-8e968d60f37f
X-Runtime 0.125778
X-Xss-Protection 1; mode=block
请求标头:
Accept text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding gzip, deflate, br
Accept-Language en-US,en;q=0.5
Connection keep-alive
Host api.nasa.gov
If-None-Match W/"e26chidden.."
Upgrade-Insecure-Requests 1
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:74.0) Gecko/999991 Firefox/74.0
【问题讨论】:
-
你有两次“响应标头”,不应该是“请求标头”吗?
-
您的 javascript 请求数据的情况如何?
-
使用浏览器开发工具中的网络窗格检查浏览器发送的所有请求和浏览器接收的所有响应。检查响应的 HTTP 状态代码。是 4xx 还是 5xx 错误而不是 200 OK 成功响应?
-
是的,第二个响应头实际上是请求头,我编辑了它。 js请求是
req.open("GET", myUrl); req.send();对api的调用很好,但是我尝试在画布中复制img,它给了我cors错误,状态码是301:Request URL:http://mars.jpl.nasa.gov/msl-raw-images/proj/msl/redops/ods/surface/sol/01000/opgs/edr/ncam/NLB_486272784EDR_F0481570NCAM00415M_.JPG Request Method:GET Remote Address:54.183.32.82:80 Status Code: 301 Version:HTTP/1.1
标签: javascript xmlhttprequest cors http-status-code-301