2

I have the following C# webservice (for testing purposes), which I eventually will turn into a WCFWebservice.

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class Albums : WebService {

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public Person GetPeople() {
        return new Person("Mike", 12);
    }
}

And I call this using the following js:

    $(document).ready(function () {
        $.ajax({
            url: '/webservice/Albums.asmx/GetPeople',
            contentType: "application/json; charset=utf-8;",
            dataType: "json",
            type: 'post',
            success: function (data) {
                console.log(data);
            }
        });
    });

But the weird thing (to me) is, I can't access data.Name within success(). Somehow, it adds a object d to data. So if I want to access the name, I need to use: data.d.Name.

Where does this d come from?

Cryothic
  • 771
  • 7
  • 18
  • As far as I know, thats just the way it is. Look at this quesion if you want to change the behaviour. http://stackoverflow.com/questions/2811525/removing-the-d-object-from-asp-net-web-service-json-output – Steve Mar 19 '14 at 09:44

2 Answers2

2

It is nothing to worry about. It comes from using the OData protocol on the server:

This pattern ensures JSON payloads returned from OData services are valid JSON statements, but not valid JavaScript statements. This prevents an OData JSON response from being executed as the result of a cross site scripting (XSS) attack.

To find out the nitty-gritty, see http://www.odata.org/documentation/odata-version-2-0/json-format.

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
1

This is done by default for both OData formatting and also as an security measure. You can easily remove this by adding the following to the ScriptMethod:

BodyStyle = WebMessageBodyStyle.Bare
rbrundritt
  • 16,570
  • 2
  • 21
  • 46