Google Place API - 请求的资源上不存在“Access-Control-Allow-Origin"标头.因此不允许访问原点 'null' [英] Google Place API - No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access

查看:19
本文介绍了Google Place API - 请求的资源上不存在“Access-Control-Allow-Origin"标头.因此不允许访问原点 'null'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Google Place API.

我想得到关于打字帮助的地方的建议.

所以,我所做的是——

var Google_Places_API_KEY = "AIzaSyAK08OEC-B2kSyWfSdeCzdIkVnT44bcBwM";//从 - https://code.google.com/apis/console/?noredirect#project:647731786600:access 获取var 语言 = "en";//'en' 代表英语,'nl' 代表荷兰语var Auto_Complete_Link = "https://maps.googleapis.com/maps/api/place/autocomplete/json?key="+Google_Places_API_KEY+"&types=geocode&language="+language+"&input=Khu";$.getJSON(Auto_Complete_Link , 函数(结果){$.each(result, function(i, field){//$("div").append(field + " ");//警报(我+=="+字段);console.error(i + "=="+ 字段);});});

所以我请求的链接是 -

<块引用>

中检查了这个问题的解决方案并浏览了 此处此处,但无法完美解决我的问题.

谁能给我指点一下?

解决方案

AJAX 请求只有在发送方和接收方的端口、协议和域相等时才可能,否则可能导致 CORS.CORS 代表跨域资源共享,必须在服务器端支持.

<块引用>

解决方案

JSONP

JSONP 或带填充的 JSON"是一种通信技术,用于在 Web 浏览器中运行的 JavaScript 程序,以从不同域中的服务器请求数据,由于同源策略,典型 Web 浏览器禁止这种技术.

这样的事情可能会帮助你交配.. :)

$.ajax({网址:Auto_Complete_Link,类型:获取",数据类型:'jsonp',缓存:假,成功:功能(响应){警报(响应);}});

I am using Google Place API.

What I want to get suggestion of place for type helping.

So, what I have done is-

var Google_Places_API_KEY = "AIzaSyAK08OEC-B2kSyWfSdeCzdIkVnT44bcBwM";      //Get it from - https://code.google.com/apis/console/?noredirect#project:647731786600:access
var language = "en";        //'en' for English, 'nl' for Nederland's Language


var Auto_Complete_Link = "https://maps.googleapis.com/maps/api/place/autocomplete/json?key="+Google_Places_API_KEY+"&types=geocode&language="+language+"&input=Khu";
$.getJSON(Auto_Complete_Link , function(result)
{
    $.each(result, function(i, field)
    {
        //$("div").append(field + " ");
        //alert(i + "=="+ field);
        console.error(i + "=="+ field);
    });
});

So in what link I am requesting is -

https://maps.googleapis.com/maps/api/place/autocomplete/json?key=AIzaSyAK08OEC-B2kSyWfSdeCzdIkVnT44bcBwM&types=geocode&language=en&input=Khu

And if I go to this link with browser, I can get output like it (please try to ck)-

But if I try with jQuery's .getJSON or .ajax, I am getting my request blocked with this message-

.

SO the XMLHTTPRequest is blocked because of -

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

I have checked in StackOverflow for this problem solution and go through here and here, but can't get my solution perfectly.

Can anyone please enlighten me?

解决方案

AJAX Requests are only possible if port, protocol and domain of sender and receiver are equal,if not might lead to CORS. CORS stands for Cross-origin resource sharing and has to be supported on the server side.

Solution

JSONP

JSONP or "JSON with padding" is a communication technique used in JavaScript programs running in web browsers to request data from a server in a different domain, something prohibited by typical web browsers because of the same-origin policy.

Something like this might help you mate.. :)

$.ajax({
            url: Auto_Complete_Link, 
            type: "GET",   
            dataType: 'jsonp',
            cache: false,
            success: function(response){                          
                alert(response);                   
            }           
        });    

这篇关于Google Place API - 请求的资源上不存在“Access-Control-Allow-Origin"标头.因此不允许访问原点 'null'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆