【发布时间】:2016-11-23 01:53:18
【问题描述】:
一直在寻找解决这个问题的方法:
Fetch API 无法加载 https://api.wunderground.com/api/******/conditions/q/toronto。 “Access-Control-Allow-Origin”标头的值“http://www.wunderground.com”不等于提供的原点。因此,Origin 'http://localhost:3000' 不允许访问。让服务器发送带有有效值的标头,或者,如果不透明的响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用 CORS 的资源。
const apiKey = '******'
const apiUrl = 'https://api.wunderground.com/api/' + apiKey + '/conditions/q/'
var WeatherApi = {
get: function(query) {
return fetch(apiUrl + query).then(function(response) {
return response.json();
});
}
};
handleClick: function() {
WeatherApi.get(this.state.text).then(function(data) {
console.log(data);
}.bind(this));
},
那么我如何让服务器发送带有有效值的标头?
【问题讨论】:
-
专门设置为wunderground.com 只允许该域,你确定它是公共api吗?
-
我确定它是一个公共 api。我从他们的文档中得到了网址:wunderground.com/weather/api/d/docs
-
@matmo,当我尝试链接中的代码时遇到同样的错误。
-
谷歌搜索 CORS。这是一种痛苦。如果您在 chrome 中执行此操作,则预检请求必须返回 200。解决方法是使用您自己的服务器使用 curl 或其他方式获取数据
标签: javascript reactjs cors wunderground