如何修复将请求的模式设置为“no-cors"? [英] How to fixed set the request's mode to 'no-cors'?

查看:516
本文介绍了如何修复将请求的模式设置为“no-cors"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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

推荐答案

您可以像这样将请求模式设置为 'no-cors':

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

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

但是,正如下面评论中的 goto1 所描述的,它通常不是您正在寻找的解决方案(请参阅他关于如何正确使用 CORS 的评论).

But, as goto1 described in the comment below, it will usually not be the solution you are looking for (see his comment on how to properly work with CORS).

这篇关于如何修复将请求的模式设置为“no-cors"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆