views:

48

answers:

1

Hi

I was looking at the 14 days of jquery

http://jquery14.com/day-01/jquery-14

and I saw this and it got me to wondering is there a point to use getJson anymore?

JSON and script types auto-detected by content-type (jQuery.ajax Documentation, Commit 1, Commit 2)

If the response to an Ajax request is returned with a JSON mime type (application/json), the dataType defaults to “json” (if no dataType is specified). Additionally, if the response to an Ajax request is returned with a JavaScript mime type (text/javascript or application/x-javascript) , the dataType defaults to “script” (if no dataType is specified), causing the script to automatically execute.

First I can see such a huge benefit of this. In jquery 1.3 I came to a situation where in some cases I would return a partial view and some cases I would return a json result (asp.net mvc).

It worked in firefox but in no other browser and one of the problems was I basically had to tell jquery to either do json or text/html.

With it automatically detecting I could get away with this. Anyways I found a solution around this at that time.

So now it just makes me wonder if there is any point to using GetJson.

I am also unsure how to set these JavaScript mime types? I am assuming that if you return a JsonResult from asp.net mvc it will set it. but I am not sure if I was just sending a text result if it would be set( I am not sure if ContentResult would set this).

+2  A: 

It is still useful as a shorthand method if all you need is the functionality offered by .getJson(). Personally, I usually end up using .ajax() for its customizability most of the time anyway.

EDIT: J-P brings up a good point in the comments that as a shorthand method, with autodetection of MIME types, .getJson is essentially rendered redundant to .get since $.getJson(a,b) would do the exact same thing as $.get(a,b).

Bradley Mountford
"if all you need is the functionality offered" - What functionality exactly? `$.getJSON` is the same as `$.get(a, b, c, "json")`
J-P
@J-P good point. with autodetection of MIME types, .getJson is essentially rendered redundant to .get since $.getJson(a,b) would do the exact same thing as $.get(a,b).
Bradley Mountford
So what I am hearing is it is pretty pointless to use getJSON from 1.4 on?
chobo2
Yup, that about sums it up.
Bradley Mountford