我看過這個問題被多個人問過,沒有任何答案對我有用。Google Maps API:請求的資源上沒有'Access-Control-Allow-Origin'標頭
我正在嘗試使用react/axios調用Google Maps API。
這是我的GET請求:
componentDidMount() {
axios({
method : 'get',
url : `http://maps.googleapis.com/maps/api/js?key=${key}/`,
headers: {
"Access-Control-Allow-Origin": '*'
"Access-Control-Allow-Methods": 'GET',
},
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
這是錯誤消息:
XMLHttpRequest cannot load http://maps.googleapis.com/maps/api/js?
key=xxxxxxxxx/. Response to preflight request doesn't pass access control
check: No 'Access-Control-Allow-Origin' header is present on the
requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
我讀過的文章CON CORS其他人都點 https://www.html5rocks.com/en/tutorials/cors/
但我無法在那裏找到我的問題的答案。