javascript - 谷歌跨源请求

标签 javascript google-api xmlhttprequest cors

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

相关文章:

node.js - 从 Google 云端硬盘中的特定文件夹中列出其中的文件夹和文件

javascript - 407 需要代理身份验证

jquery - 使用 jquery 访问 Limesurvey API - 错误 631 : failed to parse request

javascript - Bootstrap 3 图像自动调整全屏大小和滚动效果

javascript - React useState 与 webpack-dev-server 一起使用时未定义

javascript - 同步两个 Angular ui bootstrap 轮播

javascript - jQuery 触发器在 Firefox 上不起作用

google-maps - 通过 Google Maps Engine API 教程工作 - 创建表示例中的错误?

c# - 将视频上传到 YouTube - 超出配额错误

php - 无法使 xmlhttprequest 发送方法正常工作