Sure.
$.jsonp() is a kind of $.ajax clone but specific to jsonp (your url does not
need to have an =? and/or you don't have to provide a callback function name
for it to actually do a jsonp). It also offers the possibility to abort the
request just like you would with any ajax call.

So...

First, and that was the main goal originally, you can have an error callback
that actually gets called if there was a network failure or ill-formed json
responded. For instance, if you test the following url:
http://gdata.youtube.com/feeds/api/users/dcibishtho0djj77?callback=?&alt=json-in-script
$.ajax() will never notify you and you'd have a dangling script tag in your
document. $.jsonp(), on the other hand, calls the error callback as soon as
the 404 is received (no need for timeout).

Second, when the provider imposes the name of the callback (that is you
can't override it with a url parameter). $.jonsp() will work exactly the
same, you just have to set the "callback" option to  the correct value. Now
you can do that with $.ajax but the nice thing about $.jsonp is that you can
have as many jsonp calls running concurrently as you want with the exact
same callback name and there will be no collision and no need for a global
function with spaghetti code and request filtering. For instance:
$.json({
  url="url1",
  callback="globalCallback",
  success = function() { alert("First one"); }
});
$.json({
  url="url2",
  callback="globalCallback",
  success = function() { alert("Second one"); }
});
will work as expected no matter in which order the $.json calls are made nor
in which order the responses are received.

Third and a nice consequence of the second point, $.jsonp(), by default,
always uses the same callback function name ("C") which means that when you
set the cache option to true, you will actually enable browser caching where
$.ajax can't by design because it auto-generates a different callback name
at each new request.

Fourth, in case the provider is unable to issue 303 because it does not
detect the callback name hasn't changed from one call to another (a usual
design flaw in current services), you can use the pageCache option. $.jsonp
will keep a map or url => reponse and not issue a call for a url you already
requested (whether the original response was a success or an error).

You can find examples of what I said above:
http://code.google.com/p/jquery-jsonp/wiki/TipsAndTricks

To summarize, $.jsonp() is specifically designed with jsonp in mind based on
my own misadventures using usually browser unfriendly data apis. It
aknowledges the very different nature of a jsonp request and a traditionnal
ajax one and, rather than hacking on top of the $.ajax() function (like my
old getJSON plugin did), it provides an entirely new function which provides
the same options as $.ajax() does (as long as said options are relevant to a
jsonp request).

Hope this clarifies a bit,

-- Julian

2009/3/14 Dave Methvin <[email protected]>

>
> > Just a note to let you know I finally released my jsonp plugin for
> jquery.
>
> As of 1.3.x, $.ajax supports jsonp. Can you summarize what this plugin
> adds?
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to