getJSON和$ .ajax之间的区别 [英] Difference between getJSON and $.ajax

查看:237
本文介绍了getJSON和$ .ajax之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从一开始我就想提一提,我知道

Right from the start i want to mention that i know THIS question has the same title as mine but that user asked a question regarding a problem he encountered.

关于差异的我的问题比较主观.我正在学习使用Jquery和Ajax,并且遇到了两种方法.现在对我来说,他们似乎都在做同样的事情. (从指定的URL获取原始JSON数据),但我敢肯定会有更大的不同.

My question about the differences is more subjective. I'm learning to use Jquery and Ajax and i came across both methods. Now to me they both seem to do the same. (get raw JSON data from a URL specified) but i'm sure there is a bigger difference.

我还注意到人们倾向于使用$ .ajax而不是getJSON,这还有原因吗?

I also noticed that people tend to use $.ajax more then getJSON, is there a reason for that aswell?

感谢您的帮助!

推荐答案

$.getJSON()

来自 http://api.jquery.com/jquery.getjson/

这是Ajax的简写功能,等效于:

This is a shorthand Ajax function, which is equivalent to:

$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});

$.ajax()

来自 http://api.jquery.com/jquery.ajax/

数据类型 (默认值:Intelligent Guess(xml,json,脚本或html)) 类型:字符串 您期望从服务器返回的数据类型.如果未指定,则jQuery将尝试根据响应的MIME类型来推断它(XML MIME类型将产生XML,在1.4中,JSON将产生JavaScript对象,在1.4脚本中,脚本将执行该脚本,而其他任何内容将是以字符串形式返回).可用的类型(以及作为第一个参数传递给成功回调的结果):

dataType (default: Intelligent Guess (xml, json, script, or html)) Type: String The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:

"json" :将响应评估为JSON并返回一个JavaScript对象.跨域"json"请求将转换为"jsonp",除非该请求的请求选项中包含jsonp:false. JSON数据以严格的方式进行解析;任何格式错误的JSON都会被拒绝,并引发解析错误.从jQuery 1.9开始,空响应也被拒绝;服务器应返回null或{}的响应. (有关正确的JSON格式,请参阅json.org.)

"json": Evaluates the response as JSON and returns a JavaScript object. Cross-domain "json" requests are converted to "jsonp" unless the request includes jsonp: false in its request options. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} instead. (See json.org for more information on proper JSON formatting.)

这表示,如果将dataType设置为JSON并且没有返回JSON,则会引发解析错误.

That says that if you set dataType to JSON and if no JSON is returned, a parse error is thrown.

因此,从文档来看,$.getJSON()等于$.ajax(),且dataType设置为"json",这意味着如果返回的内容与JSON不同,则会导致解析错误.

So judging from the docs, $.getJSON() is equal to $.ajax() with dataType set to "json", which means that if something different than JSON is returned, you end up with a parse error.

因此,您几乎完全可以认为两者大致相同:). $.getJSON()只是更广泛的$.ajax()的简写.

So you were mostly right about the two being pretty much the same :). $.getJSON() is just shorthand for the more extensive $.ajax().

这篇关于getJSON和$ .ajax之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆