javascript 如何修复将请求的模式设置为“no-cors”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54301686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:22:54  来源:igfitidea点击:

How to fixed set the request's mode to 'no-cors'?

javascriptreactjsoauth

提问by Neel Shah

I am currently using yahoo weather api to fetch weather data. I am getting below error. Please help.

我目前正在使用雅虎天气 api 来获取天气数据。我得到低于错误。请帮忙。

Access to fetch at 'https://weather-ydn-yql.media.yahoo.com/forecastrss?location=sunnyvale,ca&format=json' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

从源“ http://localhost:3000”访问“ https://weather-ydn-yql.media.yahoo.com/forecastrss?location=sunnyvale,ca&format=json”已被 CORS 政策阻止:响应预检请求未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。如果不透明响应满足您的需求,请将请求的模式设置为“no-cors”以在禁用 CORS 的情况下获取资源。

I have tried to do google, but none of solution worked for me

我试过做谷歌,但没有一个解决方案对我有用

回答by yagiro

You can set the request mode to 'no-cors' like so:

您可以将请求模式设置为“no-cors”,如下所示:

fetch(url, {
    mode: "no-cors",
    ...
})