我一直运行到错误No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
Axios公司没有访问控制允许来源
我知道,这是一个经常被问到的问题在网络上,但我在这里没有看到有帮助的答案上比其他计算器使用JSONP的解决方法。
我的代码是
componentDidMount() {
if (navigator.geolocation){
function success(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
axios.get(`https://api.darksky.net/forecast/apikey/`+latitude+`,`+longitude+`?units=auto`)
.then(result => {
this.setState({
daily: result.data.daily.data,
loading: false,
error: null
});
})
.catch(err => {
// Something went wrong. Save the error in state and re-render.
this.setState({
loading: false,
error: err
});
});
};
function error() {
console.log('geolocation error')
};
navigator.geolocation.getCurrentPosition(success.bind(this), error);
}
}
如果愚蠢目标答案上的答案都不能解决您的问题,则可能无法解决。您必须遵循CORS策略,在没有XHR(又名jsonp)的情况下发出请求,或者从服务器发出请求。 –