Prog Prog - 1 year ago 87
Ajax Question

Why we use data.d while get response from ajax json call?

I am using

call from my
Html page
to call method from my
file using below code:

<script type="text/javascript">
function ajaxCall() {
var UserName = $("#<%=lblUsername.ClientID %>").text();
type: "POST",
url: 'webServiceDemo.asmx/CheckOldPassword',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify({ UserName: UserName }),
success: function (data) {
if (JSON.parse(data.d) != "success") // Why we need to use "data.d" ??
$("#passwordAvailable").attr("src", "App_Themes/NewTheme/images/deleteICN.gif");
$("#<%=txtOldPwd.ClientID %>").css({ 'border': '1px solid red' });
else {
$("#passwordAvailable").attr("src", "App_Themes/NewTheme/images/signoff.gif");

So my Question is why we need to use
? Why the all data store in
and what is

Because when I use only
it's not give me correct return value but when I used
it does.

please give me suggestions.

Server side code C#

public string CheckOldPassword(string UserName)
// code here
string sRtnValue = "success";
return sRtnValue;

is not property or variable but still I got value in


Answer Source

C# 3.5 and above will serialize all JSON responses into a variable d.

When the server sends a JSON response it will have a signature similar to this:

    "d" : {
        "variable" : "value"

With a console.log(data) inside the ajax success function you'll see the responses data structure in the browser console.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download