【问题标题】:Parameter passed from ajax call to wcf is received as null从 ajax 调用传递到 wcf 的参数被接收为 null
【发布时间】:2017-06-12 12:29:44
【问题描述】:

谁能帮帮我。从 ajax 传递到 wcf 的参数被接收为 null。我在堆栈溢出中经历了许多帖子,但找不到修复程序。我的代码 sn-p 如下。谢谢你的期待。

$.ajax({
    type: "POST",
    url: "/CustomServices/NewsService/getmyWork",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: JSON.stringify({ itemType: "category" }),
    processData: true,
    success: function (result) { alert("success " + result); },
    error: function (error) { alert("failure " + error.statusText); }
});

界面是

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "getmyWork/?itemType={itemType}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    SolutionV1.CustomServices.NewsService.DynamicItemsContext DoWork(string itemType);
};

服务方式

    public DynamicItemsContext DoWork(string itemType)
    {
        var items = RetrieveItems(itemType);
        var context = new DynamicItemsContext();
        context.Items = items.ToList();
        context.TotalCount = items.Count();
        return context;
    }

【问题讨论】:

  • 您在 url 中将 itemType 声明为查询参数,但您将其作为 POST 的正文内容传递。

标签: c# jquery ajax wcf


【解决方案1】:

您的 WCF 方法设置为 http post 方法,但您的 uri 模板建议该参数将作为 URL 上的查询字符串参数传递。

当您发出 POST 请求时,您的数据将在请求正文中传递,而不是在 URL 中。

尝试为您的方法更改 URI 模板以摆脱查询字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 2015-12-31
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多