为什么是必要的“jQuery.parseJSON”呢? [英] Why is 'jQuery.parseJSON' not necessary?

查看:135
本文介绍了为什么是必要的“jQuery.parseJSON”呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做什么与查询一个Ajax请求,不知道为什么我的反应已经是一个JS对象。

i'm doing an ajax request with query and wondering why my response is already a JS object.

如果我做了

var obj = jQuery.parseJSON(response);

OBJ为null,但我可以用'响应'作为JS对象的数组。

'obj' is null, but i can use 'response' as an array of js objects.

这是不是一个真正的问题,但我想知道这种行为。

This is not really a problem, but i would like to understand this behavior.

感谢

推荐答案

这发生在你做一个AJAX调用,并指定JSON的jQuery对您回应称jQuery.parseJSON的数据类型。事实上,你可以指定哪些功能,根据不同的数据类型设置为来电,你可以从本身的文档

This happens when you make an AJAX call and specify the dataType JSON jQuery calls jQuery.parseJSON on the response for you. In fact you can specify what function to call depending on the dataType as you can se from the documentation

转换器(增加1.5)
  地图默认值:{*文:window.String,文   HTML:真正的文字JSON:jQuery.parseJSON,文字XML:   jQuery.parseXML}数据类型为至的dataType转换器的映射。每   转换器的值是一个函数,返回的转化价值   响应

converters(added 1.5)
Map Default: {"* text": window.String, "text html": true, "text json": jQuery.parseJSON, "text xml": jQuery.parseXML} A map of dataType-to-dataType converters. Each converter's value is a function that returns the transformed value of the response

所以,如果你做出这样的呼吁

So if you make a call like this

$.ajax({
  url: yoururl,
  dataType: "json",
  success: function(data){
    //data is already a json
  }

如果您不指定数据类型的jQuery试图猜测它

If you don't specify a dataType jQuery tries to guess it

dataTypeString默认:智能猜测(XML,JSON,脚本,或   HTML)

dataTypeString Default: Intelligent Guess (xml, json, script, or html)

这是你期望从服务器返回的数据的类型。如果没有   为指定,jQuery将尝试基于MIME类型来推断它   响应(一个XML的MIME类型会产生XML,在1.4 JSON将产生   一个JavaScript对象,在1.4脚本将执行该脚本,   什么都将返回一个字符串)。可用的类型(和   作为第一个参数传递给你的成功回调的结果):

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:

XML:返回一个可以通过jQuery处理XML文档
。   HTML:返回HTML作为纯文本;包括脚本标记进行评估   当在DOM插入。 脚本:评估响应为   JavaScript和返回纯文本。通过添加禁用缓存   查询字符串参数, = [TIMESTAMP],除非到高速缓存中的URL   选项​​设置为true。注:这会变成文章到获取   远程域的请求。 JSON:评估响应作为JSON和   返回一个JavaScript对象。在jQuery的1.4 JSON数据解析的   严格的方式;任何畸形的JSON被拒绝,解析错误   抛出。 (关于适当的JSON格式的详细信息,请参阅json.org。)
  JSONP:使用JSONP的JSON块负荷。增加了一个额外   ?回调=?你的网址的结尾指定回调。禁用   缓存通过追加查询字符串参数,
的= [TIMESTAMP],以   URL除非缓存选项设置为true。
    文:纯文本   串。多,空间分隔值:
    在jQuery 1.5,jQuery的可   转换数据类型从它在Content-Type头接收到   您所需要的。例如,如果你想要一个文本响应为   作为XML,使用文字XML的数据类型。您也可以使   JSONP请求,把它收到的文本,并作为跨preTED的jQuery   XML:JSONP文字XML。同样,一个速记字符串,如JSONP   XML将首先尝试从JSONP转换为XML,并没有   ,从JSONP转换为文本,然后从文本到XML。

"xml": Returns a XML document that can be processed via jQuery.
"html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM. "script": Evaluates the response as JavaScript and returns it as plain text. Disables caching by appending a query string parameter, "=[TIMESTAMP]", to the URL unless the cache option is set to true. Note: This will turn POSTs into GETs for remote-domain requests. "json": Evaluates the response as JSON and returns a JavaScript object. In jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. (See json.org for more information on proper JSON formatting.)
"jsonp": Loads in a JSON block using JSONP. Adds an extra "?callback=?" to the end of your URL to specify the callback. Disables caching by appending a query string parameter, "
=[TIMESTAMP]", to the URL unless the cache option is set to true.
"text": A plain text string. multiple, space-separated values:
As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require. For example, if you want a text response to be treated as XML, use "text xml" for the dataType. You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml." Similarly, a shorthand string such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from jsonp to text, and then from text to xml.

这篇关于为什么是必要的“jQuery.parseJSON”呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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