我正在尝试从我的 HTML 页面使用 Google JS API,我必须执行如下操作:
<script src="https://www.google.com/jsapi?key=MY_API_KEY"/>
但是我收到以下错误:
火狐:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://www.google.com/jsapi?key=MY_API_KEY. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
Chrome :
XMLHttpRequest cannot load https://www.google.com/jsapi?key=MY_API_KEY. 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:8080' is therefore not allowed access. The response had HTTP status code 405.
据我所知,这是由浏览器对交叉请求的限制引起的,但我不知道如何修复它,所以我可以执行该请求。谁能给我提示?
最佳答案
我修改了它,改变了我发出请求的方式:
<script>
$.ajax({
url: "https://www.google.com/jsapi?key=MY_API_KEY",
crossDomain: true,
method: 'GET',
async: false,
dataType: 'script',
headers:{
"X-Requested-With":"XMLHttpRequest"
}
});
</script>
关于javascript - 谷歌跨源请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46521210/