Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need to use .d to access data returned by jQuery AJAX?

I've put together some jQuery AJAX code using some tutorials I found on the internet. I'm new to jQuery and want to learn how to do things betters. I have a coworker who put together a beautiful web application using a lot of jQuery.

The thing I'm most confused about here is: why is it necessary to use the ".d" when referring to the response of my web method and what does it stand for?

    // ASP.net C# code
    [System.Web.Services.WebMethod]
    public static string hello()
    {
        return ("howdy");
    }

// Javascript code
function testMethod() {
    $.ajax({
        type: "POST",
        url: "ViewNamesAndNumbers.aspx/hello",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            alert(msg);   // This doesn't display the response.
            alert(msg.d); // This displays the response.
        } // end success:
    }) // end $.ajax
like image 310
Vivian River Avatar asked Oct 13 '10 20:10

Vivian River


People also ask

Why do we use beforeSend in Ajax?

The beforeSend() function use to set the custom headers and all, it is an Ajax event that triggers before an Ajax request is started. The false value returning in the beforeSend() function will cancel the Ajax request. Whatever the type of ajax request is there the beforeSend() function is always called.

Why do we use processData in Ajax?

processData. If set to false it stops jQuery processing any of the data. In other words if processData is false jQuery simply sends whatever you specify as data in an Ajax request without any attempt to modify it by encoding as a query string.

What is data D in Ajax?

data is the JSON object returned from the endpoint. d is one of the properties of it, you are checking if the d property of data is equal to the string 'success'.

Which property is used to retrieve the data sent from the server in Ajax?

AJAX - Server Response The status property and the statusText property holds the status of the XMLHttpRequest object. Holds the status of the XMLHttpRequest. The onreadystatechange function is called every time the readyState changes.


2 Answers

It was added in ASP.NET 3.5’s version of ASP.NET AJAX to prevent you from being vulnerable to this exploit: http://haacked.com/archive/2009/06/25/json-hijacking.aspx

(Answer sourced from http://encosia.com/2009/06/29/never-worry-about-asp-net-ajaxs-d-again/)

like image 102
Samuel Cole Avatar answered Sep 28 '22 11:09

Samuel Cole


Microsoft does this to protect you from a security exploit. See the bottom of This Page for more information.

like image 40
Mike Avatar answered Sep 28 '22 11:09

Mike