简体   繁体   English

我是否应该在1.4.2中仍使用jquery .getJson?

[英]Should I still be using jquery .getJson in 1.4.2?

I was looking at the 14 days of jquery 我在看jQuery的14天

http://jquery14.com/day-01/jquery-14 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? 我看到了这一点,这让我想知道是否还有使用getJson的意义?

JSON and script types auto-detected by content-type (jQuery.ajax Documentation, Commit 1, Commit 2) JSON和脚本类型按内容类型自动检测(jQuery.ajax文档,提交1,提交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). 如果对Ajax请求的响应返回的是JSON mime类型(application / json),则dataType默认为“ json”(如果未指定dataType的话)。 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. 此外,如果使用JavaScript哑剧类型(文本/ javascript或application / x-javascript)返回了对Ajax请求的响应,则dataType默认为“脚本”(如果未指定dataType),从而使脚本自动执行。

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). 在jquery 1.3中,我遇到一种情况,在某些情况下,我将返回部分视图,而在某些情况下,我将返回json结果(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. 它可以在firefox中工作,但不能在其他浏览器中使用,问题之一是我基本上不得不告诉jquery做json或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. 所以现在让我想知道是否有使用GetJson的任何意义。

I am also unsure how to set these JavaScript mime types? 我也不确定如何设置这些JavaScript MIME类型? I am assuming that if you return a JsonResult from asp.net mvc it will set it. 我假设如果您从asp.net mvc返回JsonResult,它将对其进行设置。 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). 但是我不确定是否只设置了文本结果(不确定ContentResult是否设置了此结果)。

It is still useful as a shorthand method if all you need is the functionality offered by .getJson(). 如果您只需要.getJson()提供的功能,它仍然可用作速记方法。 Personally, I usually end up using .ajax() for its customizability most of the time anyway. 就个人而言,无论如何大多数时候我通常都会使用.ajax()来实现可自定义。

EDIT: JP 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). 编辑: JP在注释中提出了一个很好的观点,即作为一种自动检测MIME类型的速记方法,.getJson本质上对.get变得多余,因为$ .getJson(a,b)会做与$完全相同的事情。得到(a,b)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM