繁体   English   中英

从ajax调用传递到wcf的参数被接收为null

[英]Parameter passed from ajax call to wcf is received as null

有人可以帮我吗。 从ajax传递给wcf的参数被接收为null。 我经历了堆栈溢出中的众多帖子,但找不到解决方法。 我的代码段如下。 谢谢您的期待。

$.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;
    }

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM