51

I have a .NET webmethod that I have called from jQuery. The method returns some HTML markup that I display within a DIV element.

Once I have the response I use

$("#div").html(result.d);

My question is, what does the .d do? I don't like using code I don't fully understand? Could I get the same result using Eval?

2

9 Answers 9

25

Are you referring to the ADO.NET Data Services?

I remember hearing a presentation about the JSON returning this and I think its just a wrapper to ensure the payload is a JSON object as opposed to an array (which is the case of returning multiple entities).

Why 'd' specifically? I think I remember them saying something like 'well it had to be something'.

5
  • thats true, I guess it could be a , b or c but they choose d ( no reason behind this I guess) just a security feature that Microsoft added in ASP.NET 3.5 + version of ASP.NET AJAX by encapsulating the JSON response within a parent object. Commented Dec 21, 2015 at 15:49
  • 7
    I think 'd' for data
    – xr280xr
    Commented Nov 18, 2016 at 21:07
  • 3
    Is there a reason to get rid of this or change it? It's unsettling to have so much JS depending on this random "d" parameter that you don't control. What if some version changes it to "f"? Commented Jan 4, 2017 at 23:27
  • @mmcrae One year later and I have the same concern, any updates? Did you ever find out? Commented Jul 12, 2018 at 6:11
  • @NicoBerrogorry - the code I wrote that has result.d all over is still running and working in Production shrug. I guess it's just part of doing a AJAX call with a Web Forms web result end point. The "related" links in comments under the OP question I think are good for discussion of what the .d is from. --- as usual, if only there was better DOCUMENTATION... Commented Jul 12, 2018 at 16:57
12

Based on this tutorial: JSON Web Service And jQuery with Visual Studio 2008

The Web Method returns a Product that is serialized in JSON format. Since there is not JSON type, the returned value is a String with JSON format.

On the client side, the ajax call returns a JSON.

The result looks like {d: 'returned-string-with-JSON-format'}

More exactly something like: {d:'{"ID":123,"Name":"Surface Pro 2"}'}

Note that 'returned-string-with-JSON-format' is a string not a JSON object so you cannot do result.d.ID.

Instead you need to convert it to JSON object by using JSON.parse(result.d) or eval(result.d)

At the end, what you really want is do this:

result = JSON.parse(result.d)

UPDATE Also consider this demo, where I use a JSON in string format and convert it to JSON object:

enter image description here

2
2

ASPX Code Here:

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <script type="text/javascript">


        function GetData()
        {
            alert("I am called");
                $.ajax({
                    type: "POST",
                    url: "Contact.aspx/GetProducts",
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (result) {


                       var data = JSON.parse(result.d)
                       alert(data.Id);

                    },
                    error:function(ex)
                    {
                        alert("Test");
                    }
                });
        }
    </script>

     <asp:TextBox ID="txtName" runat="server"></asp:TextBox>

    <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="GetData();" />
</asp:Content>

C# Code Here:

public partial class Contact : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindList();
            }
            int[] arr1 = new int[] { 1, 2 };
            ListBox1.SelectedValue = "1";
            ListBox1.SelectedValue = "4";


        }

        void BindList()
        {

            List<Product> lst = new List<Product>()
            {
                new Product{Id=1,Name="Photo"},
                new Product{Id=2,Name="Photo"},
                new Product{Id=3,Name="Photo"},
                new Product{Id=4,Name="Photo"}
            };
            ListBox1.DataSource = lst;
            ListBox1.DataTextField = "Name";
            ListBox1.DataValueField = "Id";
            ListBox1.DataBind();
        }


        [WebMethod]
        public static string GetProducts()
        {
            // instantiate a serializer
            JavaScriptSerializer TheSerializer = new JavaScriptSerializer();

            //optional: you can create your own custom converter
           // TheSerializer.RegisterConverters(new JavaScriptConverter[] { new MyCustomJson() });

            //var products = context.GetProducts().ToList();
            Product products = new Product() { Id = 1, Name = "Testing Services" };
            var TheJson = TheSerializer.Serialize(products);

            return TheJson;
        }

    }
2
  • 1
    It is always helpful to add some explanation to your code
    – Bowdzone
    Commented Jun 5, 2015 at 8:38
  • Why should I read this? Is it an answer, an explanation, an example of the problem? Commented Jul 12, 2018 at 16:58
1

may be very much useful link for those who want to really learn from scratch an example about wrapper class where the details in one class can never be dislosed to other class but can be indirectly accessed through various methods http://www.c-sharpcorner.com/Blogs/12038/wrapper-class-in-C-Sharp.aspx

0

It returns the value of the field named 'd' in the object 'result'.

This question shows an example of how the JSON might look, notice the d: field.

0

The d is part of the result returned by your .NET code. If you look at this code you should see a variable being set with the name d. If it is generated from serialized classes, then it probably sends along a member of that class with the name d.

2
  • My method just returns a string, e.g. return "blablabla". Where is the d set?
    – Fermin
    Commented May 6, 2009 at 15:20
  • 1
    Is it possible to change or get rid of the "d"? Commented Jan 4, 2017 at 23:28
0

As others have pointed out, it returns the "d" member of the "result" object.
If you wanted to have "d" in a variable you could use this:

var property = "d";
var value = result[property];
0

Its is very clear that $("#div").html(result.d); in your code

"result" is a object and d is property of "result".

Let explain,

if you create object like this,

var result{"id": "number", "d": "day"};

if we access the property of result is that using jquery

$("#div").html(result.d);

so we get result in html is

<html>day</html>
-1

Once, my friend told me that "d" represent "data" from response that ajax get in return(because response can contain a lot more than just simple data).

Maybe it is, maybe it isn't but still you can accept it like it is. :)

1
  • 1
    Take a look again at How to Answer to see that answers should be more complete than this
    – camille
    Commented Mar 2, 2020 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.