Reputation: 963
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?
Upvotes: 2
Views: 542
Reputation: 17954
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
Upvotes: 1
Reputation: 33139
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.
Upvotes: 2