【问题标题】:Sending XML through AJAX通过 AJAX 发送 XML
【发布时间】:2012-03-29 19:28:24
【问题描述】:

我在 jQuery 中创建了一个 xml 文档,如下所示

var xmlDocument = $('<xml/>');
var foo = $('<foo/>');
var bar = $('<bar/>');

foo.append(bar);
xmlDocument.append(foo);

并尝试将其转发到服务器。

$.ajax({
   url             :   'js/foobar.php',
   type            :   'POST',
   precessData     :   false,
   contentType     :   'text/xml',
   data            :   xmlDocument,
   sucess          :   function( data ) {
      alert('success');
   },
   error           :   function() {
      alert('failed to send ajax request');
   },
   complete        :   function() {
      alert('ajax request completed');
   }
});

即使服务器只回显“foo”,我也得到alert('ajax request completed') 而不是alert('success')。我究竟做错了什么?是我创建 xml 文档的方式还是我将其转发到服务器的方式?

没有 xml 文档的 ajax 请求工作正常,我得到了“foo”。

更新 #1

precessData 更改为 processData 并将 sucess 更改为 success 后,我会看到 failed to send ajax request 对话框。

当我将 ajax 方法中的数据参数更改为

$.ajax({
   ...
   data :   {
      data: xmlDocument
   },
   ...
});

我还收到了failed to send ajax request 对话框。

服务器端的代码应该没问题,因为它只是

<?php
echo 'foo';
?>

更新 #2

我按照 AndreasAL 的回答转换了我的字符串

// Convert to string instead of DOM Elements
xmlDocument = $("<wrap/>").append(xmlDocument).html();

// Url encode the string
xmlDocument = encodeURIComponent(xmlDocument);

但我仍然得到相同的对话框 (failed to send the ajax request)。所以我认为错误可能出现在我的 xml 文档中,并使用 AndreasAL 的答案中的代码片段覆盖了我的 xml 文档。

xmlDocument = $('<xml/>');
foo = $('<foo/>').appendTo(xmlDocument);
bar = $('<bar/>').appendTo(foo);

仍然是相同的行为。

于是我再次检查了我的 xml 文档并将其打印在一个对话框中,它看起来很好。

我想不出可能出现错误的地方...

【问题讨论】:

    标签: php javascript jquery xml


    【解决方案1】:

    编辑:

    你打错字了——不是precessData,而是processData

    $.ajax({
       url             :   'js/foobar.php',
       type            :   'POST',
       precessData     :   false, // change to processData
    

    又是sucess,应该是success


    试试:

    var xmlDocument = $('<xml/>'),
        foo = $('<foo/>').appendTo(xmlDocument),
        bar = $('<bar/>').appendTo(foo);
    

    // Convert to string instead of DOM Elements
    xmlDocument = $("<wrap/>").append(xmlDocument).html();
    
    // Url encode the string
    xmlDocument = encodeURIComponent(xmlDocument);
    
    
    $.ajax({
       url             :   'js/foobar.php',
       type            :   'POST',
       processData     :   false,
       contentType     :   'text/xml',
       data            :   xmlDocument,
       success         :   function( data ) {
          alert('success');
       },
       error           :   function() {
          alert('failed to send ajax request');
       },
       complete        :   function() {
          alert('ajax request completed');
       }
    });
    

    【讨论】:

    • 天啊,我真的打错了。但现在我得到了“发送 ajax 请求失败”对话框。
    • 也许如果你像示例 2 那样转换为字符串。并修复您的成功处理程序。
    • 我试图转换字符串,仍然是相同的行为。我还检查了我的 xml 文档,但它看起来不错。还使用代码片段覆盖我的 xml 文档,以防出现错误......仍然是相同的行为。
    • 你确定你的ajax调用返回合法的xml吗?您可以在 complete 方法中尝试提醒第二个参数 (arguments[1]) 是否显示 parser error??
    • 不再调用完整方法,但错误方法显示null
    【解决方案2】:

    您在整个过程中都在使用 jQuery 对象。

    像这样编写您的 XML,将字符串连接在一起。不将它们作为 DOM 对象。

    var xmlDocument = '<xml/>';
    xmlDocument += '<foo/>';
    xmlDocument += '<bar/>';
    

    然后贴出来,像这样

    $.ajax({
       url             :   'js/foobar.php',
       type            :   'POST',
       precessData     :   false,
       contentType     :   'text/xml',
       data            :   { 
                               data: xmlDocument //wrapped inside curly braces
                           },
    
       // Here is your spelling mistake
       success          :   function( data ) {
          alert('success');
       },
       error           :   function() {
          alert('failed to send ajax request');
       },
       complete        :   function() {
          alert('ajax request completed');
       }
    });
    

    【讨论】:

      【解决方案3】:

      最后,我决定将xml文档转换成字符串发送到服务器。

      $xmlString = $(xmlDocument).html();
      

      由于我只需要存储接收到的数据,所以我将其恢复为字符串或 xml 没有区别。

      我只需要更改我的 ajax 请求,现在一切正常。

      $.ajax({
         url             :   'js/foobar.php',
         type            :   'POST',
         data            :   'data=' + xmlString,
         success         :   function( data ) {
            alert(data);
         },
         error           :   function() {
            alert('failed to send ajax request');
         },
         complete        :   function() {
            alert('ajax request completed');
         }
      });
      

      【讨论】:

        【解决方案4】:

        我认为您的代码在成功时存在错误

        $.ajax({
           url             :   'js/foobar.php',
           type            :   'POST',
           precessData     :   false,
           contentType     :   'text/xml',
           data            :   xmlDocument,
           success          :   function( data ) {
              alert('success');
           },
           error           :   function() {
              alert('failed to send ajax request');
           },
           complete        :   function() {
              alert('ajax request completed');
           }
        });
        

        【讨论】:

          【解决方案5】:

          使用 $.parseXML 来操作 XML ,您将 xml 视为 html

          http://api.jquery.com/jQuery.parseXML/

          【讨论】:

            【解决方案6】:

            使用这个:

            data : { xml: xmlDocument }
            

            发布值需要一个键

            【讨论】:

            • 我试过了,但没有成功。我仍然只将“ajax 请求完成”作为对话框。
            猜你喜欢
            • 2014-08-05
            • 2014-09-23
            • 1970-01-01
            • 1970-01-01
            • 2014-04-06
            • 2016-11-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多