tags:

views:

55

answers:

1

Hello,

I have a WCF service that accepts requests from JQuery. Currently, I can access this service. However, the parameter value is always null. Here is my WCF Service definition:

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

Here is my JQuery call:

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

What am I doing wrong? It seems odd that I can call the service but the parameter is always null. Thank you

+2  A: 

What is the json2string doing and why are you using eval? Your ExecuteQuery function takes a single string parameter named query which could be passed like this:

$.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);
    }
});
Darin Dimitrov