Google google error with jquery ajax call ... html_attributions

I am using new google api sites 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) } }); 

I get this error: invalid label html_attributions []; I think this prevents me from seeing the output object in the console, although I can see the response being returned in the json tab in firebug

+1
source share
1 answer

It seems that api places do not support ajax so far.

It is not enough that the server responds with proper JSON. The answering machine must support JSONP and surround the JSON response with the jQuery callback . The answer should look like this:

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

The hack that JSONP makes is to interpret the response as a script, because the script-Tag is not affected by a policy of the same origin. Without a callback, you have no chance of doing this in a browser.

If it is not supported, you must execute requests from your server. .

Example server 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.");"; 

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> 

You can replace the PHP code with any other server platform and complete the necessary steps.

  • HTTP request to JSON source
  • Wrap JSON as with a callback function
0
source

All Articles