【问题标题】:How do I send XML using ajax?如何使用 ajax 发送 XML?
【发布时间】:2013-08-12 16:19:13
【问题描述】:

更新 2

在仔细阅读有关 ajax 和数据属性 Here 的文档后,它说 “对象必须是键/值对”,因此我不能只传递任何对象,而是必须包装它在某种关联数组或 JSON 中,可以是这样的 {theResp:leXmlDoc},因此 contentType:"XML" 确实可以做任何事情,我正确吗?

您好,我想通过 AJAX 将数据发送到我的 ASMX 网络服务。我的意思是想用 contentType 发送数据:“application/xml”、“text/xml”或“xml”。我似乎找不到正确的方式来格式化我的 XML 以便我的 AJAX 可以工作。如果我将它包装在 JSON 中,那么我可以毫无问题地发送它。所以我想知道是否有一种特定的方式可以形成我的 XML?这是 m AJAX:我尝试将 leXmlDoc 作为 XMl 源字符串和 Dom Document 对象传递的一些注释。

function sendToServer(leXmlDoc) {
  console.log($.isXmlDoc(leXmlDoc)); //Here I check if leXmlDoc is an XML Document, I   
  also tries using jsut the XML source string
  $.ajax({
    type: 'POST',       
    processData: false, //I also tried setting it to true
    contentType: "xml", //I also tried application/xml, and text/xml
    dataType: "xml",
    url: "/Webservices/TransferXmlData.asmx/SendingXmlToServer",              
    data: leXmlDoc, 
    success: function (data) {
        console.log(data);
    },
    error: function () {
        console.log("there is a problem sending the XML");
    }
  });
};

我的网络服务,我知道这是可行的,因为当我发送 JSON 时,它会返回我发送的数据。除非它需要不同才能接受 XML。

  <WebMethod(EnableSession:=True)> _
    Public Function SendingXmlToServer(ByVal theResp As String) As String           
        Return theREsp
    End Function

编辑

console.log(leXmlDoc) 的输出作为 XML 源字符串,我检查它是字符串类型

<?xml version="1.0" encoding="UTF-8" ?>
 <Sections> 
<Section>
    <TheGreeting>Hello</TheGreeting>
    <ThePlanet>World</ThePlanet>
    <Puncuation>!</Puncuation>      
</Section>
</Sections> 

这是作为 Dom 文档的输出,我使用 console.log($.isXmlDoc(leXmlDoc)) 检查结果为真,typeOf 是一个对象,我通过这段代码创建了 leXMLDoc,其中 theXmlFile 是上面的 XML 源字符串:

var domParser = new DOMParser();
var XmlDOM = domParser.parseFromString(theXmlFile, "application/xml");
sendToServer(XmlDOM);

console.log(leXmlDoc) 作为 dom 文档的输出是这样的

 #document
 <Sections> 
<Section>
    <TheGreeting>Hello</TheGreeting>
    <ThePlanet>World</ThePlanet>
    <Puncuation>!</Puncuation>      
</Section>
</Sections> 

我希望这会有所帮助。

更新 1

当我决定像这样控制台记录 xhr 时

   error: function (xhr, ajaxOptions, thrownError) {
        console.log(xhr);
        console.log(xhr.status);
        console.log(ajaxOptions);
        console.log(thrownError);
    }

打开对象我得到一个错误提示

responseText: "System.InvalidOperationException: Request format is invalid: xml 
↵   at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
↵   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
↵"

所以看起来格式已经磨损了。然而,当我通过导航到这样的方式直接进入网络服务时

http://localhost:52530/WebServices/TransferXmlData.asmx/

并使用 console.log(leXmlDoc (Source string)) 的输出手动调用 web 服务,然后复制和粘贴它工作正常。

【问题讨论】:

  • 那么您的 js 控制台中是否出现“发送 XML 时出现问题”的问题,对吧?
  • 哦,您在发送 XML 时遇到问题,没有收到它?对不起,我看错了你的问题,错误信息是什么?
  • @RobertRozas 是的,这是 500 内部服务器错误。
  • @KarlAnderson 请看我上面的评论。
  • 请发布console.log(leXmlDoc)的内容

标签: jquery asp.net xml ajax web-services


【解决方案1】:

我就是这样做的,我设置了一个服务器端客户端来使用 ASMX Web 服务。然后使用 Fiddler 检查它发送到 ASMX 网络服务的 SOAP 数据包。这是我需要通过 $.ajax 调用发送的确切 XML。

【讨论】:

    猜你喜欢
    • 2012-03-29
    • 1970-01-01
    • 2019-07-17
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多