views:

859

answers:

1

I have a WCF service that takes as a parameter a string value "name". This service interface looks like this...

[ServiceContract(Name = "ContactLookup", Namespace = "Search")]
public interface IAjaxResultSearcherService
{
    [OperationContract]
    [WebGet(RequestFormat = WebMessageFormat.Json)]
    Result[] SearchResultsWithNameLike(string name);
}

The service is exposed to using the following configuration...

<system.serviceModel>
 <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
 <behaviors>
  <serviceBehaviors>
   <behavior name="AjaxServiceBehavior">
    <serviceDebug
                       includeExceptionDetailInFaults="true" 
                       httpHelpPageEnabled="true"/>
    <serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
    <serviceCredentials>
     <serviceCertificate findValue="localhost" 
                                 storeLocation="LocalMachine" 
                                 storeName="My" x509FindType="FindBySubjectName"/>
    </serviceCredentials>
   </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
   <behavior name="JsonBehavior">
    <enableWebScript/>
   </behavior>
  </endpointBehaviors>
 </behaviors>
 <services>
  <service name="HelloIndigo.AjaxResultSearcherService" 
                             behaviorConfiguration="AjaxServiceBehavior">
   <endpoint address="json"
             binding="webHttpBinding"
      behaviorConfiguration="JsonBehavior"
       contract="Contract.IAjaxResultSearcherService" />
  </service>
 </services>
</system.serviceModel>

My .svc file uses the WebScriptServiceHostFactory. This seems to be working, meaning that I can connect to the service from my jquery client, but even though I set the parameter value via the ajax call, when the message gets to the service implementation, the parameter value is null.

I am calling the service via jquery like so...

$.ajax({
    url: url,
    data: { "name":"test" },
    type: "GET",
    processData: false,
    contentType: "application/json",
    timeout: 10000,
    dataType: "text",
    success: function(res) {
        alert('function successful! returned: ' + res.toString());
    },
    error: function(xhr) {
        alert('an error occurred: ' + xhr.responseText);
    }
});

I get no errors from the service call, but when I debug the process, the parameter value is null. Why won't WCF pass parameter values like I expect?

Thanks for the help.

A: 

In my trials I was trying to use JSON.org's JSON2 to "stringify" my json object and pass it to the ajax call as the data object, and had set the processData parameter to false. Once I decided to leave the data elements as native JSON objects and set processData to true, my parameter values were populated.

I am still not sure why JSON2.js's stringify did not make the data parameter valid, but at least this is working.

So, previously I had the following:

var json = JSON.stringify({ "name":"test" });
$.ajax({
    url: url,
    data: json,
    type: "GET",
    processData: false,
    contentType: "application/json",
    timeout: 10000,
    dataType: "text",
    success: function(res) {
        alert('function successful! returned: ' + res.toString());
    },
    error: function(xhr) {
        alert('an error occurred: ' + xhr.responseText);
    }
});

The working version, however looks like this...

var json = { "name":"test" };
$.ajax({
    url: url,
    data: json,
    type: "GET",
    processData: true,
    contentType: "application/json",
    timeout: 10000,
    dataType: "text",
    success: function(res) {
        alert('function successful! returned: ' + res.toString());
    },
    error: function(xhr) {
        alert('an error occurred: ' + xhr.responseText);
    }
});
SteveBering