Javascript jQuery ajax 函数中 contentType 和 dataType 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14322984/
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
Differences between contentType and dataType in jQuery ajax function
提问by AndreaNobili
I have the following Jquery callback function and I have a litle doubt about it (I don't know very well Jquery):
我有以下 Jquery 回调函数,我对此有点怀疑(我不太了解 Jquery):
$("form.readXmlForm").submit(function() {
// Riferimento all'elemento form che ha scatenato il submit
var form = $(this);
// Variabile che contiene il riferimento al bottone clickato
var button = form.children(":first");
$.ajax({ // Viene eseguita la chiamata AJAX
type: "POST", // Tipo di richiesta: POST
// URL verso quale viene inviata la richiesta
url: form.attr("action"),
// Dati XML inviati:
data: "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><javaBean><foo>bar</foo><fruit>apple</fruit></javaBean>",
// Tipo di media type accettabile dalla response:
contentType: "application/xml",
dataType: "text",
success: function(text) {
MvcUtil.showSuccessResponse(text, button);
},
error: function(xhr) {
MvcUtil.showErrorResponse(xhr.responseText, button);
}
});
As you can see this function simply execute an AJAX Request to the backend setting the parameter for this request.
如您所见,此函数只需执行一个 AJAX 请求到后端设置此请求的参数。
I have set that I am sending the request towards an URL, that the request is a POST request and that the data that I am sending are the following string:
我已经设置我向 URL 发送请求,请求是一个 POST 请求,我发送的数据是以下字符串:
"barapple"
“苹果”
I have some difficulties to understand what is the differences between contentTypeand dataType
我在理解contentType和dataType之间有什么区别时遇到了一些困难
I think that contentTypespecify the type of data that are acceptable recived in the HTTP Response, is it right?
我认为contentType指定了 HTTP 响应中可接受的数据类型,对吗?
And dataType? What say? The type of data that I am sending in the HTTP Request?
和数据类型?说啥?我在 HTTP 请求中发送的数据类型?
In this case is is "text" because I am sending a textual string that rappresent XML code?
在这种情况下是“文本”,因为我正在发送一个文本字符串,它代表 XML 代码?
回答by Anthony Grist
From the documentation:
从文档:
contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8')
Type: String
When sending data to the server, use this content type. Default is "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases. If you explicitly pass in a content-type to $.ajax(), then it'll always be sent to the server (even if no data is sent). 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.
contentType(默认值:'application/x-www-form-urlencoded; charset=UTF-8')
类型:字符串
向服务器发送数据时,请使用此内容类型。默认为“application/x-www-form-urlencoded; charset=UTF-8”,这在大多数情况下都适用。如果您显式地将内容类型传递给 $.ajax(),那么它将始终发送到服务器(即使没有发送数据)。如果没有指定字符集,数据将使用服务器的默认字符集传输到服务器;您必须在服务器端对此进行适当的解码。
and:
和:
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).
数据类型(默认:智能猜测(xml、json、脚本或 html))
类型:字符串
您期望从服务器返回的数据类型。如果没有指定,jQuery 将尝试根据响应的 MIME 类型推断它(XML MIME 类型将产生 XML,在 1.4 中 JSON 将产生一个 JavaScript 对象,在 1.4 脚本中将执行脚本,其他任何类型都将以字符串形式返回)。
They're essentially the opposite of what you thought they were.
它们本质上与您认为的相反。
回答by Maria Ines Parnisari
In English:
用英语:
ContentType: When sending data to the server, use this content type. Default isapplication/x-www-form-urlencoded; charset=UTF-8, which is fine for most cases.Accepts: The content type sent in the request header that tells the server what kind of response it will accept in return. Depends onDataType.DataType: 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. Can betext, xml, html, script, json, jsonp.
ContentType: 向服务器发送数据时,使用此内容类型。默认为application/x-www-form-urlencoded; charset=UTF-8,这适用于大多数情况。Accepts: 在请求头中发送的内容类型,它告诉服务器它将接受什么样的响应作为回报。取决于DataType.DataType:您期望从服务器返回的数据类型。如果没有指定,jQuery 将尝试根据响应的 MIME 类型推断它。可以text, xml, html, script, json, jsonp。


