【问题标题】:return list<string> from WCF service called by jQuery从 jQuery 调用的 WCF 服务返回 list<string>
【发布时间】:2011-10-04 03:26:16
【问题描述】:

我的服务有以下调用返回list&lt;string&gt;

当我运行它时,我收到错误已发生的消息。

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

【问题讨论】:

  • 你能包含错误信息吗?
  • 我想知道你为什么使用 POST 来检索数据?
  • @ladislav - 有什么不同吗?
  • @Andre - 我收到一个消息框,指出:发生错误 - [object XMLHttpRequest]。然后在 Firebug 中只显示 500 Internal Server Error。

标签: c# jquery wcf json


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多