1

My goal is to acquire just the business reviews from a Google places API call, I expected to see the full Json results stringified or returned otherwise in the console log. But instead I get a error in console that appears to be trying to treat the json response as executable code. Am i returning it to the console in a improper way or is this normal?

Ajax Call

var getReviews = $.ajax({
                type: "POST",
                url: 'https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJc5Kt8ow574gRi2G-KSJOtnY&key=A VALID API KEY',
                async:true,
                dataType : 'jsonp',   //used jsonp for cross origin request
                crossDomain:true,
                success: function(data, textStatus, jqXHR) { console.log(JSON.stringify(data)); }
                });

error:

enter image description here

Partial of Json response when posted straight to browser URL:

{
   "html_attributions" : [],
   "result" : {
      "address_components" : [
         {
            "long_name" : "1955",
            "short_name" : "1955",
            "types" : [ "street_number" ]
         },
         {
            "long_name" : "Southwest Main Boulevard",
            "short_name" : "SW Main Blvd",
            "types" : [ "route" ]
         },
Kevin B
  • 94,570
  • 16
  • 163
  • 180
DMSJax
  • 1,709
  • 4
  • 22
  • 35
  • `crossDomain:true,` Why? `type: "POST",` Why? Do a little bit of research on what exactly JSONP means. – Kevin B Aug 05 '16 at 20:28
  • 1
    *"But instead I get a error in console that appears to be trying to treat the json response as executable code"* **Yes, that's how jsonp gets around the same-origin policy.** – Kevin B Aug 05 '16 at 20:28
  • If you change your success function to `console.log(data.d);`, what do you get? – Tyler Roper Aug 05 '16 at 20:29
  • @TylerRoper nothing because the success function isn't being called. – Kevin B Aug 05 '16 at 20:29
  • Possible duplicate of [jQuery AJAX JSONP error "Unexpected token"](http://stackoverflow.com/questions/14853267/jquery-ajax-jsonp-error-unexpected-token) – Kevin B Aug 05 '16 at 20:31
  • @KevinB I see. His question (citing that the error was in the response) made me think that the error was happening on his `stringify` portion. – Tyler Roper Aug 05 '16 at 20:31
  • @KevinB easy there killer, it was just a question about the console behavior and if it was normal error in response because jsonp being used, which is new to me. For what it's worth, i did review [link] (http://stackoverflow.com/questions/3839966/can-anyone-explain-what-jsonp-is-in-layman-terms) but didn't interpret that post to explain why console log behaved that way. – DMSJax Aug 05 '16 at 20:58
  • the console.log() isn't even being called. – Kevin B Aug 05 '16 at 20:58
  • well, it's in the success callback and since the "error" is displaying a part of the json RESULT, i assumed it was succesfully getting the data returned, but somehow console.log was having some weird parsing error – DMSJax Aug 05 '16 at 21:00
  • It's also unsafe to include your API key on ANY client-side code. anyone can read that from the network console. The only way to hide it would be to not use this request in the browser and instead use the appropriate javascript sdk that google provides for this service. – Kevin B Aug 05 '16 at 21:02
  • I know. One step at a time though - get through the result problem first. i'll fix the client facing data after. – DMSJax Aug 05 '16 at 21:03
  • but... you can't fix the first problem. unless you work for google or want to fix problem 2 at the same time by not using the browser to do this. – Kevin B Aug 05 '16 at 21:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120281/discussion-between-dmsjax-and-kevin-b). – DMSJax Aug 05 '16 at 21:04

0 Answers0