什么是jQuery ajax Post中的contentType和dataType以及数据?

时间:2022-10-17 22:56:52

I have just started to Learn the Json and binding data to Gridview using Json but I am not able to understand what is the contentType and dataType and data ?

我刚开始学习Json并使用Json将数据绑定到Gridview,但我无法理解contentType和dataType和数据是什么?

I used the Following code............

我使用了以下代码............

<script type="text/javascript">
$(document).ready(function () {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "Gridview.aspx/BindDatatable",
        data: "{}",
        dataType: "json",
        success: function (data) {
            for (var i = 0; i < data.d.length; i++) {
                $("#gvDetails").append("<tr><td>" + data.d[i].OfficeName + "</td><td>" + data.d[i].City + "</td><td>" + data.d[i].Country + "</td></tr>");
            }
        },
        error: function (result) {
            alert("Error");
        }
    });
});
</script>

2 个解决方案

#1


14  

The contentType referres to the mime content type that specifies the type of content set to the server. This could indentify FORM-Encoded, XML, JSON and a plethora of other content types. It helps the server to determine how to handle the content.

contentType指的是指定设置到服务器的内容类型的mime内容类型。这可以识别FORM-Encoded,XML,JSON和许多其他内容类型。它有助于服务器确定如何处理内容。

dataType helps JQuery with regards to how to handle the data. if specifying json then the returned data will be evaluated as json and data passed to the success handler will be an object instead of a string

dataType帮助JQuery处理数据。如果指定json,那么返回的数据将被计算为json,传递给success处理程序的数据将是一个对象而不是一个字符串

The data property is used for data passed to The server. If you pass in an Object literal. JQuery will pass it either as part of the request body (if type is post) or as part of the query string (if type is get)

data属性用于传递给服务器的数据。如果传入Object文字。 JQuery将它作为请求体的一部分传递(如果类型为post)或作为查询字符串的一部分(如果type为get)

#2


3  

if we specify the data type as Json then the returned data will be evaluated as Json and data passed to the success handler will be an object instead of string,Lets see the example

如果我们将数据类型指定为Json,那么返回的数据将被评估为Json,并且传递给成功处理程序的数据将是一个对象而不是字符串,让我们看看示例

  $.ajax({
        type: "POST",
        url: "ProductWebService.asmx/GetProductDetails",

    data: "{'productId':'" + productId + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {
        var Product = response.d;
        $("#spnProductId").html(Product.Id);strong text
        $("#spnProductName").html(Product.Name);
        $("#spnPrice").html(Product.Price);
        $("#outputTable").show();
    },
    failure: function (msg) {
        alert(msg);
    }
});

#1


14  

The contentType referres to the mime content type that specifies the type of content set to the server. This could indentify FORM-Encoded, XML, JSON and a plethora of other content types. It helps the server to determine how to handle the content.

contentType指的是指定设置到服务器的内容类型的mime内容类型。这可以识别FORM-Encoded,XML,JSON和许多其他内容类型。它有助于服务器确定如何处理内容。

dataType helps JQuery with regards to how to handle the data. if specifying json then the returned data will be evaluated as json and data passed to the success handler will be an object instead of a string

dataType帮助JQuery处理数据。如果指定json,那么返回的数据将被计算为json,传递给success处理程序的数据将是一个对象而不是一个字符串

The data property is used for data passed to The server. If you pass in an Object literal. JQuery will pass it either as part of the request body (if type is post) or as part of the query string (if type is get)

data属性用于传递给服务器的数据。如果传入Object文字。 JQuery将它作为请求体的一部分传递(如果类型为post)或作为查询字符串的一部分(如果type为get)

#2


3  

if we specify the data type as Json then the returned data will be evaluated as Json and data passed to the success handler will be an object instead of string,Lets see the example

如果我们将数据类型指定为Json,那么返回的数据将被评估为Json,并且传递给成功处理程序的数据将是一个对象而不是字符串,让我们看看示例

  $.ajax({
        type: "POST",
        url: "ProductWebService.asmx/GetProductDetails",

    data: "{'productId':'" + productId + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {
        var Product = response.d;
        $("#spnProductId").html(Product.Id);strong text
        $("#spnProductName").html(Product.Name);
        $("#spnPrice").html(Product.Price);
        $("#outputTable").show();
    },
    failure: function (msg) {
        alert(msg);
    }
});