我在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
将processData更改为processDatasuccess后,我将获得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文档,并将其打印在对话框中,它看起来很好。
我想不出错误可能在哪里了。。。
编辑:
你有一个打字错误——不是precessData
,而是processData
$.ajax({
url : 'js/foobar.php',
type : 'POST',
precessData : false, // change to processData
并且再次在CCD_ 8中,该CCD_
尝试:
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');
}
});
您在整个过程中都在使用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');
}
});
最后,我决定转换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');
}
});
我认为您的代码在成功上有一个错误
$.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');
}
});
使用$.parseXML来操作XML,您将XML视为html
http://api.jquery.com/jQuery.parseXML/
使用这个:
data : { xml: xmlDocument }
Post值需要一个密钥