Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is contentType and dataType and data in jQuery ajax Post?

Tags:

json

c#

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 ?

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>
like image 994
Kartik Patel Avatar asked May 25 '12 10:05

Kartik Patel


1 Answers

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.

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

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)

like image 124
Rune FS Avatar answered Oct 12 '22 05:10

Rune FS