2012-03-13 52 views
3

發送XML創建jQuery的XML文檔如下通過AJAX

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

即使服務器回聲一個「富」而已,我得到的alert('ajax request completed')而不是alert('success')。我究竟做錯了什麼?這是我創建xml文檔的方式還是我將其轉發到服務器的方式?

沒有xml文檔的ajax請求工作正常,我得到'foo'回來。

更新#1

改變precessData過程數據sucess成功我得到的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答案中的代碼snipplet覆蓋了我的xml文檔。

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

仍然是相同的行爲。

所以我再次檢查我的xml文件,並將其打印在對話框中,它看起來很好。

我正在用盡錯誤的想法......

回答

2

最後,我決定轉換XML文檔並將其作爲字符串發送到服務器。

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

由於這樣的事實,我只需要存儲的數據收到,它沒有什麼區別,如果我revieving它作爲字符串或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'); 
    } 
}); 
0

使用本:

data : { xml: xmlDocument } 

郵政值需要一個鍵

+0

我試過了,但沒有解決。作爲對話框,我仍然只獲得'ajax請求已完成'。 – 2012-03-13 11:53:05

4

編輯:

你有一個O型這不是precessDataprocessData

$.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'); 
    } 
}); 
+0

omg,我真的誤會了它。但是現在我得到'未能發送ajax請求'對話框。 – 2012-03-13 11:56:57

+0

你有沒有試過@Bas Tuijnman說? – andlrc 2012-03-13 12:10:58

+0

我也試過了,仍然是同一個對話框。 – 2012-03-13 12:31:12

1

我覺得你有

$.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'); 
    } 
}); 
3

您正在使用jQuery對象上的成功在您的代碼中的錯誤通過整個proc ESS。

像這樣編寫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'); 
    } 
});