gpt4 book ai didi

javascript - 谷歌跨源请求

转载 作者:行者123 更新时间:2023-11-30 15:01:08 25 4
gpt4 key购买 nike

我正在尝试从我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com