JQuery和WCF - GET方法传递null

时间:2010-04-25 15:47:23

标签: jquery wcf

我有一个接受来自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似乎很奇怪。谢谢

1 个答案:

答案 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);
    }
});