1

I have a problem in showing an alert box inside an onsuccess function called by $.ajax. I have checked firebug, and I do receive a response in json format but for some reason the the alert is coming neither am I able to console.log(jsonp). Below is the code:

$.ajax({

        type: "GET",
        url: "http://maps.googleapis.com/maps/api/directions/json?origin=ajax&destination=toronto&region=ca&avoid=tolls&sensor=false",
        dataType: "jsonp",
        success: function(jsonp) {
              alert(jsonp);
            console.log(jsonp);

        }
    });

});
scunliffe
  • 62,582
  • 25
  • 126
  • 161
Khurram Ijaz
  • 1,844
  • 5
  • 24
  • 43

1 Answers1

1

Read this is clearly written here : http://api.jquery.com/jQuery.ajax/

By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.

One more thing is mentioned

"jsonp": Loads in a JSON block using JSONP. Will add an extra "?callback=?" to the end of your URL to specify the callback.

If you wish to force a crossDomain request (such as JSONP) on the same domain, set the value of crossDomain to true. This allows, for example, server-side redirection to another domain

Reference Links:

PHP-JSONP

CROSS DOMAIN DATA with jsonp

JavaScript: How do I create JSONP?

Community
  • 1
  • 1
xkeshav
  • 53,360
  • 44
  • 177
  • 245
  • please explain what should i do when i am getting the response back from the server and i want to process that data which came back in json format but how can i access this data now ? – Khurram Ijaz Apr 18 '11 at 09:34
  • please read the link i provide...it is very easy..basically u have to write `?callback=` at the and of the url – xkeshav Apr 18 '11 at 09:37
  • now i am doing ?callback=myFunction but still inside myFunction i am not able to process the data no alert or log neither i can update any of Div id inside html using $(myDiv).text(jsonp_data); ? i will appreciate if you kindly mention the syntax – Khurram Ijaz Apr 18 '11 at 09:51
  • i update my answer with reference links.. please have a look..All D Best – xkeshav Apr 18 '11 at 09:56
  • Thanks i have read most of the links but still i am not able to print alert or use the data coming back from response. I can see the data is comming back in firbug. JsonP is working fine now how to use this data ? – Khurram Ijaz Apr 18 '11 at 10:11
  • sorry you are not understanding the question i am asking please understand the question first. I am getting the response back from googleapi and i see a JSON returned from the google but i am not able to alert or use its data in the call back funcction please tell me what i am missing do i have encode or decode json parse jason or some other thing to get the value out of that json came from google. – Khurram Ijaz Apr 18 '11 at 10:32
  • still unsuccessfull i dont know what am i missing. I do see JSON returned from google. in firebug , there must be some thing im missin. – Khurram Ijaz Apr 18 '11 at 11:03
  • did u try with only `alert()` .dont use 'console` – xkeshav Apr 18 '11 at 11:08
  • yes thats always the first thing. you can copy and paste it the above code and try at your end – Khurram Ijaz Apr 18 '11 at 11:25