AJAX请求中的内容类型和数据类型是什么?


178

POST请求中的内容类型和数据类型是什么?假设我有这个:

$.ajax({
    type : "POST",
    url : /v1/user,
    datatype : "application/json",
    contentType: "text/plain",
    success : function() {

    },
    error : function(error) {

    },

contentType我们发送的吗?那么我们在上面的示例中发送的是JSON,而我们接收的是纯文本?我不太了解

Answers:


303

contentType是您要发送的数据类型,application/json; charset=utf-8也是常见的一种,application/x-www-form-urlencoded; charset=UTF-8默认值为。

dataType什么是你期待从服务器返回:jsonhtmltext等jQuery将使用这个弄清楚如何填充成功函数的参数。

如果您要发布类似的内容:

{"name":"John Doe"}

并期待回来:

{"success":true}

然后,您应该具有:

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
    },
});

如果您期望以下内容:

<div>SUCCESS!!!</div>

然后,您应该执行以下操作:

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
    },
});

还有一个-如果您要发布:

name=John&age=34

然后不要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
    },
});

的确感谢您:)这是什么“成功”:是的。是后端中的另一个json文件吗?如何取得成功?这就是我真正想知道的
user2759697 2013年

2
那只是一个普通的对象-它是由服务器决定的。Web服务器可以发送任何感觉到的内容-HTML,文本,或者在这种情况下,是具有名称为“ success”且值为true的单个属性的JSON对象。我无法猜测你的API的框架是什么,但在C#中的ASP.NET MVC这将是一些简单的[HttpPost]public JsonResult user(Person postedPerson) { /* Save postedPerson to DB */ return Json(new { success = true }); }
乔·伊诺斯

1
请注意,您应该使用 $.ajax({ dataType : "html", ... 而不是$.ajax({ datatype : "html",... 单词Type中的大写T 来代替 。检查jQuery API
Vadim Levkovsky

1
@Jacques我想他们可以做requestContentTyperesponseDataType,但在现实中,一旦你已经做了几次,你懂的API,你将不会被混淆不足以使额外的输入值得的。
Joe Enos

1
@stom这个问题和我的答案是特定于POST的,但是如果我记得,如果您将普通的简单对象作为GET上的数据传递,它将把它变成带有键值对的查询字符串。不知道如果您有一个带有嵌套值的复杂对象,会发生什么-但是如果您好奇的话,尝试它应该不难。不过,我永远不会在现实生活中这样做-很少我会在GET上使用AJAX。
Joe Enos

31

从jQuery文档-http://api.jquery.com/jQuery.ajax/

contentType将数据发送到服务器时,请使用此内容类型。

dataType您期望从服务器返回的数据类型。如果未指定任何内容,则jQuery将尝试根据响应的MIME类型进行推断

“文本”:纯文本字符串。

因此,您希望contentType为,application/json而dataType为text

$.ajax({
    type : "POST",
    url : /v1/user,
    dataType : "text",
    contentType: "application/json",
    data : dataAttribute,
    success : function() {

    },
    error : function(error) {

    }
});

的确感谢,这个应用程序在application / json中是什么。这是一条路吗?
user2759697 2013年

1
@ user2759697这只是JSON定义的MIME类型的一部分。看到这个问题- stackoverflow.com/questions/477816/...
理查德·道尔顿

4
这就是我所喜欢的...表达出明显的观点而获得代表...> _ <
Christoph

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.