google 使用 jquery ajax 调用放置 api 错误... html_attributions [英] google places api error with jquery ajax call... html_attributions

查看:33
本文介绍了google 使用 jquery ajax 调用放置 api 错误... html_attributions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 jquery/ajax 的新 google 场所 api.当我运行此代码时:

I'm using the new google places api with jquery/ajax. When I run this code:

$.ajax({
url: "https://maps.googleapis.com/maps/api/place/search/json?location=40.7834345,-73.9662495&radius=50&sensor=false&key=Your_API_KEY_HERE",
dataType: "jsonp",
data: {
    name: 'rogue'
},
success: function( data ) {
     console.log(data)
}
});

我收到此错误:标签无效 html_attributions [];我认为这使我无法在控制台中看到输出对象,尽管我可以在 firebug 的 json 选项卡中看到响应正常返回

I get this error: invalid label html_attributions []; I think this is preventing me from seeing the output object in the console, although I can see the response coming back fine in the json tab in firebug

推荐答案

目前似乎地方 api 不支持 ajax.

It seems like the places api does not support ajax so far.

服务器以正确的 JSON 响应是不够的.应答服务器必须支持 JSONP 并用 jQuery 生成的回调包围 JSON 应答.响应必须如下所示:

It not enough that the server responds with proper JSON. The answering server has to support JSONP and surround the JSON answer with a callback generated by jQuery. The response must look like that:

jQuery17101705844928510487_1324249734338({"data":"whatever"});

JSONP 所做的 hack 是将响应解释为脚本,因为脚本标签不受同源策略的影响.如果没有回调,您将无法在浏览器中执行此操作.

The hack that JSONP does, is to interpret the response as script, because the script-Tag is not affected by the Same-Origin-Policy. Without the callback you have no chance to do that in the browser.

如果它不受支持,您必须从您的服务器执行请求..

If its not supported you have to do the requests from your server..

使用 PHP 的服务器示例:

Server-Example with PHP:

<?php
header("Content-Type:text/javascript"); // avoid browser warnings
$request = new HttpRequest("http://programmingisart.com/json-data-source.php", HttpRequest::METH_GET);
$request->send();
$json_data = $request->getResponseBody();

// wrap the data as with the callback
$callback = isset($_GET["callback"]) ? $_GET["callback"] : "alert";
echo $callback."(".$json_data.");";

使用 jQuery 的客户端示例:

Client-Example with jQuery:

<div id="json-result"></div>
<script type="text/javascript">
    $(document).ready(function() {
        $.ajax({
            dataType: "jsonp",
            url: "jsonp-wrapper.php",
            success: function(data) {
                $("#json-result").html(JSON.stringify(data));
            },
            error: function() {
                alert("error");
            }
        });
    });
</script>

您可以用任何其他服务器平台替换 PHP 代码并执行所需步骤.

  • 对 JSON 源的 HTTP 请求
  • 像回调函数一样包装 JSON

这篇关于google 使用 jquery ajax 调用放置 api 错误... html_attributions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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