下一页 JS 'Access-Control-Allow-Origin'值错误



我在部署到Netlify后注意到一个问题,chrome阻止了我对公共Apple api的ajax请求,但它对我发送链接的朋友有效。

现在它朝着相反的方向断裂了。在开发中,标题似乎是我的netlify url,而不是localhost。

Access to fetch at 'https://itunes.apple.com/search?entity=podcast&term=test&limit=6' 
from origin 
'http://localhost:3001' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' 
header has a value 'https://effulgent-dingus-192e6.netlify.app' 
that is not equal to the supplied origin. Have the server send the header with a valid value, 
or, if an opaque response serves your needs, set the request's mode to 'no-cors' 
to fetch the resource with CORS disabled.

我如何确保Access-Control-Allow-Origin没有被缓存(或解决任何问题(。

我发现NextJS在服务器端和预期的客户端都运行API调用。我将调用封装在一个检查中,以查看window !== undefined是否只在客户端进行调用。我认为这已经解决了问题。

要防止缓存,请在响应中包含Vary: Origin标头。

CORS和缓存

假设服务器发送一个响应,其中包含一个具有明确原点的访问控制允许原点值(而不是"*"通配符(。在这种情况下,响应还应该包括一个值为Origin的Vary响应标头,以向浏览器指示服务器响应可能因Origin请求标头的值而异。

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin#cors_and_caching

相关内容

最新更新