我看到很多人问过这个问题,但是没有一个答案对我有效。
我正在尝试使用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无法加载http://maps.googleapis.com/maps/api/js? key=xxxxxxxxx/. 响应预检请求未通过访问控制检查:请求的资源上没有'Access-Control-Allow-Origin'头。因此,来自'http://localhost:3000'的访问被拒绝。
我读了其他人指向的关于CORS的文章 https://www.html5rocks.com/en/tutorials/cors/
但是我在那里找不到解决我的问题的答案。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号