return list<string> from WCF service called by jQuery
I have the following call to my service that is returning a list<string>
When I run this I get the error has occured message.
$(document).ready(function () //executes this code when page loading is done
{
$.ajax({
type: "POST",
url: "Services/pilltrakr.svc/getAllUsers",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response.d开发者_如何学编程);
},
error: function (message) {
alert("error has occured : " + message);
}
});
});
How do I return a list from my WCF Service?
Interface:
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
List<string> getAllUsers();
Class:
public List<string> getAllUsers()
{
userAccount ua = new userAccount();
List<string> myUserList = new List<string>();
myUserList = ua.getAllUsers();
return myUserList;
}
UPDATE:
I changed the BodyStyle attribute to WrappedRequest and then everything worked (I tried with both GET and POST). The data was then returned. Now my question is why did that change resolve this issue? Do I always need to include a BodyStyle?
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
List<string> getAllUsers();
I changed the BodyStyle attribute to WrappedRequest and then everything worked (I tried with both GET and POST). The data was then returned. Now my question is why did that change resolve this issue? Do I always need to include a BodyStyle?
精彩评论