返回list< string>从jQuery调用的WCF服务

本文关键字:调用 WCF 服务 jQuery list string 返回 | 更新日期: 2023-09-27 18:10:25

我有以下调用我的服务,返回一个list<string>

当我运行这个时,我得到错误已经发生的消息。

$(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);
        }
    });
});

我如何从我的WCF服务返回列表?

接口:

[OperationContract]
[WebInvoke(Method = "POST",
                BodyStyle = WebMessageBodyStyle.Wrapped,
                ResponseFormat = WebMessageFormat.Json,
                RequestFormat = WebMessageFormat.Json)]
List<string> getAllUsers();
类:

public List<string> getAllUsers()
{
    userAccount ua = new userAccount();
    List<string> myUserList = new List<string>();
    myUserList = ua.getAllUsers();
    return myUserList;
}

更新:

我把BodyStyle属性改为WrappedRequest,然后一切都工作了(我尝试了GET和POST)。然后返回数据。现在我的问题是为什么这个改变解决了这个问题?我是否总是需要包含BodyStyle?

    [OperationContract]
    [WebInvoke(Method = "POST",
                    BodyStyle = WebMessageBodyStyle.WrappedRequest,
                    ResponseFormat = WebMessageFormat.Json,
                    RequestFormat = WebMessageFormat.Json)]
    List<string> getAllUsers();

返回list< string>从jQuery调用的WCF服务

我把BodyStyle属性改为WrappedRequest,然后一切都工作了(我尝试了GET和POST)。然后返回数据。现在我的问题是为什么这个改变解决了这个问题?我是否总是需要包含BodyStyle?