我有一个接受来自JQuery的请求的WCF服务。目前,我可以访问此服务。但是,参数值始终为null。这是我的WCF服务定义:
[OperationContract]
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public string ExecuteQuery(string query)
{
// NOTE: I get here, but the query parameter is always null
string results = Engine.ExecuteQuery(query);
return results;
}
这是我的JQuery调用:
var searchUrl = "/services/myService.svc/ExecuteQuery";
var json = { "query": eval("\"test query\"") };
alert(json2string(json)); // Everything is correct here
if (json != null) {
$.ajax({
type: "GET",
url: searchUrl,
contentType: "application/json; charset=utf-8",
data: json2string(json),
dataType: "json"
});
}
我做错了什么?我可以调用服务但参数始终为null似乎很奇怪。谢谢
答案 0 :(得分:3)
json2string
做什么以及为什么使用eval
?您的ExecuteQuery
函数采用名为query
的单个字符串参数,可以像这样传递:
$.ajax({
url: searchUrl,
contentType: 'application/json; charset=utf-8',
data: { query: 'this is the query that will be sent to the service' },
success: function(json) {
// json.d will contain the string result returned by the web method
alert(json.d);
}
});