简体   繁体   English

Google跨域请求

[英]Google Cross-Origin Request

I am trying to use Google JS API from my HTML page and I have to do something like: 我正在尝试从HTML页面使用Google JS API,并且必须执行以下操作:

<script src="https://www.google.com/jsapi?key=MY_API_KEY"/>

But I am getting the following error: 但是我收到以下错误:

Firefox: 火狐:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://www.google.com/jsapi?key=MY_API_KEY . 跨域请求被阻止:同源策略禁止在https://www.google.com/jsapi?key=MY_API_KEY上读取远程资源。 (Reason: CORS header 'Access-Control-Allow-Origin' missing). (原因:CORS标头“ Access-Control-Allow-Origin”缺失)。

Chrome: 铬:

XMLHttpRequest cannot load https://www.google.com/jsapi?key=MY_API_KEY . XMLHttpRequest无法加载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. 对预检请求的响应未通过访问控制检查:请求的资源上不存在“ Access-Control-Allow-Origin”标头。 Origin ' http://localhost:8080 ' is therefore not allowed access. 因此,不允许访问源' http:// localhost:8080 '。 The response had HTTP status code 405. 响应的HTTP状态码为405。

As I have read it is caused by browsers restrictions to crossed request but I have no clue about how to fix it so I can do that request. 据我了解,这是由于浏览器对交叉请求的限制所致,但是我不知道如何解决它,因此我可以执行该请求。 Can anyone give me a tip? 谁能给我小费?

I fixed it changing the way I made the request: 我将其更改为更改发出请求的方式:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM