$.get(u,d,c,"JSON") vs $.getJSON(u,d,c)
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?
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");
}
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
精彩评论