jQuery AJAX 请求中的内容类型和数据类型是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18701282/
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
What is content-type and datatype in an AJAX request?
提问by user2759697
What is content-type and datatype in a POST request? Suppose I have this:
POST 请求中的内容类型和数据类型是什么?假设我有这个:
$.ajax({
type : "POST",
url : /v1/user,
datatype : "application/json",
contentType: "text/plain",
success : function() {
},
error : function(error) {
},
Is contentType
what we send? So what we send in the example above is JSON and what we receive is plain text? I don't really understand.
contentType
我们发送的是什么?那么我们在上面的例子中发送的是 JSON 而我们接收的是纯文本?我真的不明白。
回答by Joe Enos
contentType
is the type of data you're sending, so application/json; charset=utf-8
is a common one, as is application/x-www-form-urlencoded; charset=UTF-8
, which is the default.
contentType
是您要发送的数据类型,application/json; charset=utf-8
也是常见的类型,原样application/x-www-form-urlencoded; charset=UTF-8
,这是默认值。
dataType
is what you're expecting back from the server: json
, html
, text
, etc. jQuery will use this to figure out how to populate the success function's parameter.
dataType
是您期望从服务器返回的内容:json
、html
、text
等。jQuery 将使用它来确定如何填充成功函数的参数。
If you're posting something like:
如果您要发布类似的内容:
{"name":"John Doe"}
and expecting back:
并期待回来:
{"success":true}
Then you should have:
那么你应该有:
var data = {"name":"John Doe"}
$.ajax({
dataType : "json",
contentType: "application/json; charset=utf-8",
data : JSON.stringify(data),
success : function(result) {
alert(result.success); // result is an object which is created from the returned JSON
},
});
If you're expecting the following:
如果您期待以下内容:
<div>SUCCESS!!!</div>
Then you should do:
那么你应该这样做:
var data = {"name":"John Doe"}
$.ajax({
dataType : "html",
contentType: "application/json; charset=utf-8",
data : JSON.stringify(data),
success : function(result) {
jQuery("#someContainer").html(result); // result is the HTML text
},
});
One more - if you want to post:
还有一个 - 如果你想发帖:
name=John&age=34
Then don't stringify
the data, and do:
然后不要stringify
数据,然后做:
var data = {"name":"John", "age": 34}
$.ajax({
dataType : "html",
contentType: "application/x-www-form-urlencoded; charset=UTF-8", // this is the default value, so it's optional
data : data,
success : function(result) {
jQuery("#someContainer").html(result); // result is the HTML text
},
});
回答by Richard Dalton
From the jQuery documentation - http://api.jquery.com/jQuery.ajax/
来自 jQuery 文档 - http://api.jquery.com/jQuery.ajax/
contentTypeWhen sending data to the server, use this content type.
dataTypeThe 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
"text": A plain text string.
contentType向服务器发送数据时,使用此内容类型。
dataType您期望从服务器返回的数据类型。如果没有指定,jQuery 将尝试根据响应的 MIME 类型推断它
“text”:纯文本字符串。
So you want contentType to be application/json
and dataType to be text
:
所以你想要 contentTypeapplication/json
和 dataType 是text
:
$.ajax({
type : "POST",
url : /v1/user,
dataType : "text",
contentType: "application/json",
data : dataAttribute,
success : function() {
},
error : function(error) {
}
});
回答by Jono
See http://api.jquery.com/jQuery.ajax/, there's mention of datatype and contentType there.
请参阅http://api.jquery.com/jQuery.ajax/,那里提到了 datatype 和 contentType 。
They are both used in the request to the server so the server knows what kind of data to receive/send.
它们都用于对服务器的请求,因此服务器知道要接收/发送的数据类型。