通过AJAX发送XML
我在jQuery中创建一个xml文档,如下所示
var xmlDocument = $(''); var foo = $(''); var 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并成功后 ,我得到了failed to send ajax request
对话框。
当我将ajax方法中的data参数更改为
$.ajax({ ... data : { data: xmlDocument }, ... });
我也failed to send ajax request
对话框。
服务器端的代码应该没问题,因为它是唯一的
更新#2
我按照AndreasAL的回答转换了我的字符串
// Convert to string instead of DOM Elements xmlDocument = $("").append(xmlDocument).html(); // Url encode the string xmlDocument = encodeURIComponent(xmlDocument);
但我仍然得到相同的对话框( failed to send the ajax request
)。 所以我认为错误可能在我的xml文档中,并使用AndreasAL的答案中的代码snipplet覆盖了我的xml文档。
xmlDocument = $(''); foo = $('').appendTo(xmlDocument); bar = $('').appendTo(foo);
仍然是相同的行为。
所以我再次检查了我的xml文档并将其打印在一个对话框中,看起来很好。
我的错误可能在哪里……
编辑:
你有一个类型-o它不是precessData
它的processData
$.ajax({ url : 'js/foobar.php', type : 'POST', precessData : false, // change to processData
在sucess
女巫呼喊success
尝试:
var xmlDocument = $(' '), foo = $(' ').appendTo(xmlDocument), bar = $(' ').appendTo(foo);
// Convert to string instead of DOM Elements xmlDocument = $(" ").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 Object。
像这样编写XML,将字符串连接在一起。 不将它们作为DOM对象。
var xmlDocument = ' '; xmlDocument += ' '; xmlDocument += ' ';
然后发布它,就像这样
$.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
用这个:
data : { xml: xmlDocument }
post值需要一把钥匙