2

I'm creating an iOS / Droid app using AJAX, jQuery, and Phonegap. The backend is a Drupal 7 site serving content via web services. I'm a little confused about the necessity for a JSONP callback, though.

My understanding is that JSONP is required in order to allow cross domain requests. And, in part, it works by wrapping the JSON data within a callback function, thus making it an object.

However, what I'm finding is that I can send the JSONP request without a callback, and parse the data via jQuery without the callback function. So the JSONP data looks exactly like JSON data.

Am I missing something? If providing the data as function is one of the benefits of JSONP, shouldn't it be required at some stage in the process? Am I bypassing some level of security by not sending a callback parameter?

Thanks!

FYI, here is the jQuery AJAX code:

$.ajax({
    type: 'GET',
    cache: false,
    url: 'http://www.example.com/mobile/menu/devel-gosto.jsonp',
    dataType: 'jsonp',
    timeout: menuLoadTimeout,
    success: function (data) {
        menu = populateSlider(data.tree, 0);
    }
}); 

So I'm setting the dataType to 'jsonp', calling to a web service that returns JSONP data, but leaving the callback out of the URL. The web service responds without a callback wrapper. And everything works fine.

8
  • How do you parse the JSON-not-P data with jQuery? Can you show us an example of this?
    – Bergi
    Aug 4, 2012 at 22:38
  • 1
    Is your "ajax call" to the same domain that is serving the HTML?
    – Ray Toal
    Aug 4, 2012 at 22:39
  • 1
    ...or does it support CORS? And jQuery automatically detects the MIME type and parses JSON instead of executing a script?
    – Bergi
    Aug 4, 2012 at 22:40
  • 1
    are you sure that jquery isn't automatically generating and adding the callback into the request url?
    – J. Holmes
    Aug 4, 2012 at 22:48
  • 1
    32bitkid: you solved my mystery. The server logs show that jQuery is indeed adding the callback to the URL if I don't specify it. I hadn't noticed it in Firebug. Thanks!
    – Mark
    Aug 4, 2012 at 23:26

4 Answers 4

3

A JSONP call doesn't work without a callback. The data is loaded in a script tag, and if the code is not in a form of a method call, the result would just be an object that was discarded, and the success callback method would never be called.

The ajax method is adding a callback parameter to the URL even if you don't specify one.

In the documentation, under the "jsonp" value for the dataType setting:

"Adds an extra "?callback=?" to the end of your URL to specify the callback."

http://api.jquery.com/jQuery.ajax/

1
  • Ah, I should have RTFM. Thanks, Guffa. :)
    – Mark
    Aug 4, 2012 at 23:43
3

JSONP was desinged in response to the Same-Origin Policy (SOP) which stated that if the HTML page was served from one domain, then the web page (once delivered to a client) could not make an "Ajax call" to a site on a different domain. If the "Ajax call" was made to the same domain, all is fine. (Perhaps that it was you are seeing?)

Now, you can't make Ajax calls to different domains but you can use the script tag to invoke code on a different domain (go figure, eh?). Now the thing is, suppose you made a call to a different domain in your script tag and you got back some JSON text. How would you do anything with it? This is where JSONP comes in. If the server sends back some JSON wrapped in a function call, then when you evaulate that (wrapped) object, you are now "doing something with it."

Lately though, most browsers support CORS, so JSONP is not needed. Some older browsers do not support CORS, however, but these are getting to be fewer and fewer.

1
  • Ironically, once I compile, JSON is fine because Phonegap doesn't have a cross-domain security issue. So for dev, I suppose I should look into CORS. I haven't found a reliable way to invoke it in my dev environment.
    – Mark
    Aug 4, 2012 at 23:40
1

Is a callback necessary?

Yes, definitely. The callback is substantial to the definition of JSON-with-padding. Without, it is just JSON.

0

Yes, it's required. Default function name is callback, but can be any declared function name.

https://jar-download.com/java-documentation-javadoc.php?a=jersey-json&g=com.sun.jersey&v=1.19.2

It provides us with a way to access the returned data. It does this by having the server return JSON data wrapped in a function call (the “padding”) which can then be interpreted by the browser. This function must be defined in the page evaluating the JSONP response.

see here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.