Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google Places API jQuery.ajax()请求因URL可用而失败_Jquery_Google Maps Api 3 - Fatal编程技术网

Google Places API jQuery.ajax()请求因URL可用而失败

Google Places API jQuery.ajax()请求因URL可用而失败,jquery,google-maps-api-3,Jquery,Google Maps Api 3,如果在浏览器选项卡中粘贴以下URL: https://maps.googleapis.com/maps/api/place/search/json?location=51.5237587%2C-0.1583642&radius=500&types=bar&key=MY_KEY_HERE&sensor=false 。。。我从GooglePlacesAPI获得了预期的JSON响应(当然,这里我的键被替换为实际的键,这里和下面的.ajax()中)。但是,使用此jQue

如果在浏览器选项卡中粘贴以下URL:

https://maps.googleapis.com/maps/api/place/search/json?location=51.5237587%2C-0.1583642&radius=500&types=bar&key=MY_KEY_HERE&sensor=false
。。。我从GooglePlacesAPI获得了预期的JSON响应(当然,这里我的键被替换为实际的键,这里和下面的.ajax()中)。但是,使用此jQuery.ajax()构造时:

$.ajax({
     type: 'GET',
     url: "https://maps.googleapis.com/maps/api/place/search/json",
     data: {"location" : latlng, "radius" : 500, "types" : "bar", "key" : "MY_KEY_HERE", "sensor" : "false",},
     dataType: "json",
     success: function(data)
     {
       var pubResults = data; 
     },
     error: function(data)
     {
       alert(JSON.stringify(data));
     },
     complete: function(data)
     {
       initialize($.oneapi.latitude, $.oneapi.longitude, pubResults);
     }
   }); 
…则未到达成功块,而是错误块输出:

{"readyState":0,"responseText":"","status":0,"statusText":"error"}
在Firefox5.01中测试。Web控制台确认.ajax()正在获取问题顶部提到的URL。你知道为什么jQuery调用该URL会导致错误,但粘贴到浏览器选项卡中的相同URL会导致预期的JSON吗


非常感谢您的时间

这是一个跨域请求。默认情况下,浏览器会阻止来自跨域站点的响应。您需要使用jsonp作为数据类型。只要用谷歌搜索一下,你就可以看到如何使用jQueryAPI实现这一点。堆栈溢出也有这些问题


在同源策略下,从server1.example.com提供服务的网页通常无法连接到server1.example.com以外的服务器或与之通信。HTML元素是一个例外。利用元素的开放策略,一些页面使用元素来检索Javascript代码,该代码对来自其他来源的动态生成的JSON格式数据进行操作。这种使用模式被称为JSONP。

非常感谢,我应该已经发现了!请注意,GooglePlaces不符合JSONP,因此我使用了GooglePlacesJavaScriptAPI。干杯