views:

27

answers:

1

I am trying to call a Web service to retrieve some XML data from a database. The Ajax call works fine if I use a static file e.g. like this:

$.ajax({
    type: "GET",
    url: "test2.xml",
    data: buildXMLDataRequestObject(),
    dataType: "xml",
    success: getXMLDataSucceeded,
    error: getXMLDataFailed
});

but fails when I try to call the Web service e.g. like this:

$.ajax({
    type: "POST",
    url: "Services/CheckOutService.svc/GetXMLData",
    data: buildXMLDataRequestObject(),
    dataType: "xml",
    success: getXMLDataSucceeded,
    error: getXMLDataFailed
});

The error I get is:

"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."

The GetXMLData method looks like this:

// Interface
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
string GetXMLData(XMLDataRequest request);
...
// Implementation
public string GetXMLData(XMLDataRequest request)
{
    request.ShopperId = ShopperId;
    return checkOutManager.GetXMLData(request);
}   

The GetXMLData method has been configured to return XML and the Ajax call has its datatype set as XML so I'm very confused as to what is causing the error.

EDIT: If I alter the $.ajax() call slightly so that the contentType is specified I get this error:

The data at the root level is invalid. Line 1, position 1.

I've tried contentType: "text/xml" and contentType: "application/xml" and both give the same error.

EDIT: Yesterday (Aug 30th) I noticed that the service call would succeed if I omitted the data parameter of the ajax call. I guess there is something about the JSON object that is causing a problem. For now I have implemented this functionality on the server side of the application but I do intend to revisit this when I get some time.

A: 

My first guess would be that the content type was wrong. What do you see when you look at the stream using Fiddler or similar?

Jon Hanna
Hi John,With Fiddler grabbing the traffic I can see a HTTP 500 error for Services/CheckOutService.svc/GetXMLData as expected. In the Headers tab there is an Entity item whose Content-Type is set to "application/json; charset=utf-8". Could this be the problem? Also, what do you mean by "the stream"? Is there something specific I should be looking at?
Malice
Well, you say above that you are sending XML, and that header should be for sending JSON, so there's a mismatch there. By "the stream" I mean the whole thing, the headers and the entity body and whether it matches what you expect. If you're sending XML you should be sending an XML content type, not a JSON one.
Jon Hanna
Thanks for the quick response. So if my Ajax request has its datatype set to XML and the ResponseFormat of the Web service method is set to WebMessageFormat.Xml where is the json setting coming from and more importantly, how can I change it?
Malice