2010-04-25 56 views
0

我有一个接受来自JQuery的请求的WCF服务。目前,我可以访问此服务。但是,参数值始终为空。这里是我的WCF服务的定义:JQuery和WCF - GET方法传递NULL

[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" 
    }); 
} 

我在做什么错?看起来很奇怪,我可以调用服务,但参数始终为空。谢谢

回答

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