views:

174

answers:

2

I generally use $.get() and $.post() for all of my asynchornous calls, but they're usually qualified with the final parameter being "JSON," indicating that I'm expecting to handle JSON data within my callback.

Is there any benefit to using $.get([url],[data],[callback],"JSON") over $.getJSON([url],[data],[callback])? Is it nothing more than no longer needing to include the final parameter, an explicit declaration of the return type?

+4  A: 

No difference. It's obvious from the jQuery source. I use getJSON for all cross domain calls and get when calls follow same origin policy.

getJSON: function( url, data, callback ) {
  return jQuery.get(url, data, callback, "json");
}
Chandra Patni
Thanks for the quick answer. And thank you also for introducing me to this much more exhaustive and detailed presentation of the jQuery source.
Jonathan Sampson
+2  A: 

As @Chandra pointed out, it is a convenience method. I checked the source as well to be sure, and it simply calls $.get. So, the only performance of $.get over $.getJSON is there would be one less method call. However, since it seems to be clearer, I would say that using $.getJSON should be preferred over $.get

Doug Neiner
+1 I'm a man of brevity - I suppose that is why I preferred `$.get()` over and against `$.getJSON()`, but I agree that from a clearer and more readable perspective, `$.getJSON()` makes more sense when you're "getting JSON." Thanks, Doug!
Jonathan Sampson
What gets me is everyone using `$.ajax("GET"...` requests. `$.get` and `$.post` is just way more clear and easy to write. Glad I could help!
Doug Neiner