I am trying to get data for JQGrid from a WCF web service running from my ASP.NET 2.0 WebForms application. The problem is that the WCF web service expects the data to be formatted as a JSON string, and the JQGrid executes an HTTP message and passes it as Content-Type: application / x-www-form-urlencoded.
Although there are several options for the data format returned by JQGrid (it accepts JSON, XML, and others), there seems to be no way to change the way data is sent to the web service.
So, I'm trying to figure out how to configure the WCF service to accept
Content-Type: application/x-www-form-urlencoded
but not
Content-Type:"application/json; charset=utf-8"
When I ran a test using jQuery to send an Ajax request using url encoding (shown here):
$.ajax({ type: "POST", url: "../Services/DocLookups.svc/DoWork", data: 'FirstName=Howard&LastName=Pinsley', contentType: "Content-Type: application/x-www-form-urlencoded", dataType: "json", success: function(msg) { alert(msg.d); } });
the call fails. Using Fiddler to check traffic, I found the error returned by the server:
{"ExceptionDetail":{"HelpLink":null,"InnerException":null,"Message": "The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details."...
Please note that this code works due to the difference in encoding
$.ajax({ type: "POST", url: "../Services/DocLookups.svc/DoWork", data: '{"FirstName":"Howard", "LastName":"Pinsley"}', contentType: "application/json; charset=utf-8", dataType: "json", success: function(msg) { alert(msg.d); } });
On the server, the service looks like this:
[ServiceContract(Namespace = "")] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] [ServiceBehavior(IncludeExceptionDetailInFaults = true)] public class DocLookups {
and my web.config contains:
<system.serviceModel> <behaviors> <endpointBehaviors> <behavior name="DocLookupsAspNetAjaxBehavior"> <enableWebScript /> </behavior> </endpointBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> <services> <service name="DocLookups"> <endpoint address="" behaviorConfiguration="DocLookupsAspNetAjaxBehavior" binding="webHttpBinding" contract="DocLookups" /> </service> </services> </system.serviceModel>
Thanks for any help!