简体   繁体   English

从ASP.NET Web服务检索XML数据时出现问题

[英]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. 我试图调用Web服务以从数据库中检索一些XML数据。 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: 但是当我尝试调用Web服务时失败,例如:

$.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." “传入的消息具有意外的消息格式'Raw'。该操作的期望消息格式为'Xml','Json'。这可能是因为尚未在绑定上配置WebContentTypeMapper。有关更多信息,请参见WebContentTypeMapper的文档。细节。”

The GetXMLData method looks like this: GetXMLData方法如下所示:

// 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. 已将GetXMLData方法配置为返回XML,并且Ajax调用将其数据类型设置为XML,因此对于导致错误的原因我非常困惑。

EDIT: If I alter the $.ajax() call slightly so that the contentType is specified I get this error: 编辑:如果我稍稍更改$ .ajax()调用,以便指定contentType,我将收到此错误:

The data at the root level is invalid. 根级别的数据无效。 Line 1, position 1. 第1行,位置1。

I've tried contentType: "text/xml" and contentType: "application/xml" and both give the same error. 我已经尝试了contentType:“ text / xml”和contentType:“ application / xml”,并且都给出了相同的错误。

EDIT: Yesterday (Aug 30th) I noticed that the service call would succeed if I omitted the data parameter of the ajax call. 编辑:昨天(8月30日),我注意到如果省略ajax调用的data参数,该服务调用将成功。 I guess there is something about the JSON object that is causing a problem. 我想关于JSON对象的某些问题会引起问题。 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? 使用Fiddler或类似工具查看流时会看到什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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