笑故挽风 2013-08-12 16:19 采纳率: 100%
浏览 27

如何使用Ajax发送XML?

Update 2

After carefully reading the doc about ajax and the data property Here it says that "Object must be Key/Value pairs", so therefore I can not just pass any object but must wrapp it in some kind of associative array, or JSON, which can be something like this {theResp:leXmlDoc}, so therefore contentType:"XML" does really do anything am I correct?

Hi I would like to send data via AJAX to my ASMX webservice. By that I mean want to send data with the contentType: "application/xml", "text/xml" or "xml". I cannot seem to find the right way to format my XML so that my AJAX can work. If I wrap it inside a JSON then I am able to send it without any problem. So I was wondering is there a specific way I am suppose to form my XML? Here is m AJAX: A few notes I tries passing leXmlDoc as a XMl source string and as an Dom Document object.

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");
    }
  });
};

My webservice, I know this works because when I send a JSON it return me the data I sent it. Unless it needs to be different to accept XML.

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

EDIT

The output of console.log(leXmlDoc) as the XML source string, I check and it is of type string

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

Here is the out put as a Dom Document, I check using console.log($.isXmlDoc(leXmlDoc)) and the result is true, typeOf is an object and I created the leXMLDoc by this bit of code where theXmlFile is the XML source string from above:

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

The output of console.log(leXmlDoc) as a dom Document is this

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

I hope this helps.

UPDATE 1

When I decided to console.log the xhr like this

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

and open up the object I get an error that says

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

So it seems that the format is worng. Yet when I go directly to the webservice by navigating to like this

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

And manually invoke the webservice with the output of console.log(leXmlDoc (Source string)), and then copying and pasting it works fine.

  • 写回答

1条回答 默认 最新

  • weixin_33697898 2014-02-24 10:10
    关注

    This is how I did it, I setup a server side client for using the ASMX webservice. Then using Fiddler inspected the SOAP packet it sent to the ASMX webserivce. This was the exact XML that I needed to send via $.ajax call.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!