jQuery $.ajax - 数据类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2722750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:08:47  来源:igfitidea点击:

$.ajax - dataType

jquery

提问by Nick Kahn

What is the difference between

之间有什么区别

contentType: "application/json; charset=utf-8",
dataType: "json",

vs.

对比

contentType: "application/json",
dataType: "text",

回答by Nick Craver

  • contentTypeis the HTTP headersent to the server, specifying a particular format.
    Example: I'm sending JSON or XML
  • dataTypeis you telling jQuery what kind of responseto expect.
    Expecting JSON, or XML, or HTML, etc. The defaultis for jQuery to try and figure it out.
  • contentType是发送到服务器的 HTTP标头,指定特定格式。
    示例:我要发送 JSON 或 XML
  • dataType你是在告诉 jQuery期望什么样的响应
    期待 JSON,或 XML,或 HTML 等。默认是 jQuery 尝试并找出它。

The $.ajax()documentation has full descriptions of these as well.

$.ajax()文件具有这些完整描述为好。



In your particular case, the first is asking for the responseto be in UTF-8, the second doesn't care. Also the first is treating the responseas a JavaScript object, the second is going to treat it as a string.

在您的特定情况下,第一个是要求响应在 中UTF-8,第二个不在乎。此外,第一个将响应视为 JavaScript 对象,第二个将其视为字符串。

So the first would be:

所以第一个是:

success: function(data) {
  // get data, e.g. data.title;
}

The second:

第二:

success: function(data) {
  alert("Here's lots of data, just a string: " + data);
}

回答by nash era

(ps: the answer given by Nick Craver is incorrect)

(ps:Nick Craver给出的答案是错误的)

contentType specifies the format of data being sent to the server as part of request(it can be sent as part of response too, more on that later).

contentType 指定作为请求的一部分发送到服务器的数据格式(它也可以作为响应的一部分发送,稍后会详细介绍)。

dataType specifies the expected format of data to be received by the client(browser).

dataType 指定客户端(浏览器)接收的数据的预期格式。

Both are not interchangable.

两者不可互换。

  • contentTypeis the header sent to the server, specifying the format of data(i.e the content of message body) being being to the server. This is used with POST and PUT requests. Usually when u send POST request, the message body comprises of passed in parameters like:
  • contentType是发送到服务器的标头,指定发送到服务器的数据格式(即消息体的内容)。这与 POST 和 PUT 请求一起使用。通常,当您发送 POST 请求时,消息正文包含传入的参数,例如:

==============================

==============================

Sample request:

样品请求:

POST /search HTTP/1.1 
Content-Type: application/x-www-form-urlencoded 
<<other header>>

name=sam&age=35

==============================

==============================

The last line above "name=sam&age=35" is the message body and contentType specifies it as application/x-www-form-urlencoded since we are passing the form parameters in the message body. However we aren't limited to just sending the parameters, we can send json, xml,... like this(sending different types of data is especially useful with RESTful web services):

“name=sam&age=35”上方的最后一行是消息正文,contentType 将其指定为 application/x-www-form-urlencoded 因为我们在消息正文中传递表单参数。然而,我们不仅限于发送参数,我们还可以发送 json、xml 等等(发送不同类型的数据对于 RESTful Web 服务特别有用):

==============================

==============================

Sample request:

样品请求:

POST /orders HTTP/1.1
Content-Type: application/xml
<<other header>>

<order>
   <total>9.02</total>
   <date>December 22, 2008 06:56</date>
...
</order>

==============================

==============================

So the ContentType this time is: application/xml, cause that's what we are sending. The above examples showed sample request, similarly the response send from the server can also have the Content-Type header specifying what the server is sending like this:

所以这次的 ContentType 是:application/xml,因为这就是我们要发送的内容。上面的示例显示了示例请求,类似地,从服务器发送的响应也可以具有 Content-Type 标头,指定服务器发送的内容,如下所示:

==============================

==============================

sample response:

样本响应:

HTTP/1.1 201 Created
Content-Type: application/xml
<<other headers>>

<order id="233">
   <link rel="self" href="http://example.com/orders/133"/>
   <total>9.02</total>
   <date>December 22, 2008 06:56</date>
...
</order>

==============================

==============================

  • dataTypespecifies the format of response to expect. Its related to Accept header. JQuery will try to infer it based on the Content-Type of the response.
  • dataType指定期望的响应格式。它与 Accept 标头有关。JQuery 将尝试根据响应的 Content-Type 推断它。

==============================

==============================

Sample request:

样品请求:

GET /someFolder/index.html HTTP/1.1
Host: mysite.org
Accept: application/xml
<<other headers>>

==============================

==============================

Above request is expecting XML from the server.

上面的请求期望来自服务器的 XML。

Regarding your question,

关于你的问题,

contentType: "application/json; charset=utf-8",
dataType: "json",

Here you are sending json data using UTF8 character set, and you expect back json data from the server. As per the JQuery docs for dataType,

在这里,您使用 UTF8 字符集发送 json 数据,并且您希望从服务器返回 json 数据。根据数据类型的 JQuery 文档,

The json type parses the fetched data file as a JavaScript object and returns the constructed object as the result data.

json 类型将获取的数据文件解析为 JavaScript 对象,并将构造的对象作为结果数据返回。

So what you get in success handler is proper javascript object(JQuery converts the json object for you)

所以你在成功处理程序中得到的是正确的 javascript 对象(JQuery 为你转换 json 对象)

whereas

然而

contentType: "application/json",
dataType: "text",

Here you are sending json data, since you haven't mentioned the encoding, as per the JQuery docs,

在这里您发送的是 json 数据,因为您没有提到编码,根据 JQuery 文档,

If no charset is specified, data will be transmitted to the server using the server's default charset; you must decode this appropriately on the server side.

如果没有指定字符集,数据将使用服务器的默认字符集传输到服务器;您必须在服务器端对其进行适当的解码。

and since dataType is specified as text, what you get in success handler is plain text, as per the docs for dataType,

并且由于 dataType 被指定为文本,因此您在成功处理程序中得到的是纯文本,根据 dataType 的文档,

The text and xml types return the data with no processing. The data is simply passed on to the success handler

text 和 xml 类型返回未经处理的数据。数据被简单地传递给成功处理程序

回答by SilentGhost

as per docs:

根据文档

  • "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.)
  • "text": A plain text string.
  • "json":将响应计算为 JSON 并返回一个 JavaScript 对象。在 jQuery 1.4 中,JSON 数据以严格的方式解析;任何格式错误的 JSON 都会被拒绝并引发解析错误。(有关正确 JSON 格式的更多信息,请参阅 json.org。)
  • "text": 纯文本字符串。

回答by Atif Hussain

jQuery Ajax loader is not working well when you call two APIs simultaneously. To resolve this problem you have to call the APIs one by one using the isAsyncproperty in Ajax setting. You also need to make sure that there should not be any error in the setting. Otherwise, the loader will not work. E.g undefined content-type, data-type for POST/PUT/DELETE/GET call.

当您同时调用两个 API 时,jQuery Ajax 加载器无法正常工作。要解决此问题,您必须使用isAsyncAjax 设置中的属性一个一个地调用 API 。您还需要确保设置中不应有任何错误。否则,加载器将无法工作。例如,未定义的内容类型、POST/PUT/DELETE/GET 调用的数据类型。