我一直運行到錯誤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)的情況下發出請求,或者從服務器發出請求。 –