【发布时间】: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