【发布时间】:2011-08-28 07:51:46
【问题描述】:
我有一个带有 3 个字符串参数的 WCF 服务。用 jQuery 和 JSON 调用它确实到达了我的方法,但只有 1 个参数包含一个值 - 其他参数被接收为 null,即使它们被传递。有什么想法吗?
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public string Save(string site, string title, string contentType)
{
// method...
}
JS:
$.ajax(
{
type: "POST",
url: "/Service.svc/Save",
dataType: "json",
data: "{\"title\": \"title...\", \"site\": \"site...\", \"contentType\": \"contentType...\"}",
contentType: "application/json; charset=utf-8",
success: function(data) {
},
error: function() {
alert("Sorry, an error has occured");
}
}
【问题讨论】:
-
您是否尝试过更正参数的顺序?您的服务方法接受订单站点、标题和内容类型中的参数,但您在订单标题、站点和内容类型中发送它。不确定这是否会解决它,但值得一试。
-
是的,对不起,我应该提到这一点。参数的顺序不会改变结果。标题是唯一接收到值的参数。
-
在我尝试时有效。我在 VS2010 中创建了一个 WCF 服务应用程序,我的 JQuery 版本是 1.5.1。这是一个预感:重命名站点和 contentType 名称。
-
您是否在客户端对参数进行编码?