最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why we use data.d while get response from ajax json call? - Stack Overflow

programmeradmin0浏览0评论

I am using AJAX call from my Html page to call a method from my asmx.cs file, using below code:

    <script type="text/javascript">
        function ajaxCall() {               
            var UserName = $("#<%=lblUsername.ClientID %>").text();                
            $("#passwordAvailable").show();
            $.ajax({
                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");
                    }
                }                    
            });
        }
    </script>

So my Question is why do we need to use data.d ? Why does all data is being stored in .d and what is .d?

Because when I use only data it's not giving me correct return values but when I used data.d it does.

please give me suggestions.

Server side code C#

[WebMethod]
    public string CheckOldPassword(string UserName)
    {
// code here
string sRtnValue = "success";
return sRtnValue;
}

so d is not property or variable but still I got value in .d Thanks

I am using AJAX call from my Html page to call a method from my asmx.cs file, using below code:

    <script type="text/javascript">
        function ajaxCall() {               
            var UserName = $("#<%=lblUsername.ClientID %>").text();                
            $("#passwordAvailable").show();
            $.ajax({
                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");
                    }
                }                    
            });
        }
    </script>

So my Question is why do we need to use data.d ? Why does all data is being stored in .d and what is .d?

Because when I use only data it's not giving me correct return values but when I used data.d it does.

please give me suggestions.

Server side code C#

[WebMethod]
    public string CheckOldPassword(string UserName)
    {
// code here
string sRtnValue = "success";
return sRtnValue;
}

so d is not property or variable but still I got value in .d Thanks

Share Improve this question edited Jul 20, 2020 at 10:40 Warios 3492 silver badges17 bronze badges asked Nov 3, 2014 at 9:45 prog1011prog1011 3,4954 gold badges33 silver badges59 bronze badges 7
  • Can't answer without the JSON code. – Scimonster Commented Nov 3, 2014 at 9:47
  • 1 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'. – Rich Bradshaw Commented Nov 3, 2014 at 9:47
  • 1 You can answer without the JSON code... The server is returning JSON with a variable named d – David Barker Commented Nov 3, 2014 at 9:49
  • .d is not any property or variable please see the updated post – prog1011 Commented Nov 3, 2014 at 9:53
  • 1 c# 3.5 and above puts all data into the variable 'd'. – David Barker Commented Nov 3, 2014 at 9:57
 |  Show 2 more comments

1 Answer 1

Reset to default 16

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.

发布评论

评论列表(0)

  1. 暂无评论