Why does web service return data as msg.d

I found that my webmethod is returning data as

{ "d":
 [
   {"id":"1","itemtxt":"Masters"},
   {"id":"2","itemtxt":"Transactions"},
   {"id":"3","itemtxt":"Misch. Reports"}
 ]
}

If you notice, the array is named as “d“. Why is that ? is there any rule for it?

For your information I am returning a list of objects (List<webitem>)

public class webitem
{
    public webitem(string kid, string kval)
    {
        this.id = kid;
        this.itemtxt = kval;
    }

    public string id { get; set;  }
    public string itemtxt { get; set; }
}

What does this “d” mean ? will it allways be same for whatever data i send from my webmethod? or it is going to change depending on my data type/class type?

EDIT

Here is the webmethod

[WebMethod]
public  List<webitem> GetSubModules(string key)
{
    string s = " order by smid";
    if (key != null)
        s = " where moduleid=" + key + s;
    return Utility.GetDDLVal("Select smid id,smname itemtxt from m_submodules "+s, null, null);
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

It’s a bit of a security feature as part of ASP.NET. It will always be there as long as you don’t change serializers. David Ward’s blog (now defunct) said what it is:

This is the case with all ASMX services JSON serialized through the ASP.NET AJAX Extensions in ASP.NET 3.5. Even if you’re only returning a scalar return value, such as a string, int, or boolean, the result will always be enclosed within the “d”.

And the why:

{“d”: 1 }

Is not a valid JavaScript statement, where as this:
[1]
Is.

So the wrapping of the “d” parameter prevents direct execution of the string as script. No Object or Array constructor worries.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x