發送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文件,並將其打印在對話框中,它看起來很好。
我正在用盡錯誤的想法......
我試過了,但沒有解決。作爲對話框,我仍然只獲得'ajax請求已完成'。 – 2012-03-13 11:53:05