简体   繁体   中英

Problem retrieving XML data from an ASP.NET Web service

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM