【发布时间】: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 的正文内容传递。