通过AJAX发送XML

问题描述:

我在jQuery中创建一个xml文档,如下所示

I create a xml document in jQuery as following

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

foo.append(bar);
xmlDocument.append(foo);

并尝试将其转发到服务器。

and try to forwards it to the server.

$.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',我也会得到提醒('ajax request completed')而不是警报('成功')。我究竟做错了什么?这是我创建xml文档的方式还是我将其转发到服务器的方式?

Even if the server echos a 'foo' only, I get the alert('ajax request completed') and not the alert('success'). What am I doing wrong? Is it the way I'm creating the xml document or is it the way I forward it to the server?

没有xml文档的ajax请求正常工作,我收回'foo'。

An ajax request without a xml document works fine and I get the 'foo' back.

更新#1

precessData 更改为 processData 成功 成功我得到无法发送ajax请求对话框。

After changing precessData to processData and sucess to success i get the failed to send ajax request dialog.

当我将ajax方法中的data参数更改为

When I change the data parameter in the ajax method to

$.ajax({
   ...
   data :   {
      data: xmlDocument
   },
   ...
});

我也得到未能发送ajax请求对话框。

服务器端的代码应该没问题,因为它只是

The code on the server side should be fine cause it's only

<?php
echo 'foo';
?>

更新#2

我在AndreasAL的回答中转换了我的字符串

I converted my string as in AndreasAL's answer

// Convert to string instead of DOM Elements
xmlDocument = $("<wrap/>").append(xmlDocument).html();

// Url encode the string
xmlDocument = encodeURIComponent(xmlDocument);

但我仍然得到相同的对话框(无法发送ajax请求)。所以我认为错误可能在我的xml文档中,并使用AndreasAL答案中的代码snipplet覆盖了我的xml文档。

but i still get the same dialog box (failed to send the ajax request). So i thought the error could be in my xml document and overwrote my xml document by using the code snipplet from AndreasAL's answer.

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

行为仍然相同。

所以我再次检查了我的xml文档并将其打印在一个对话框中,它看起来很好。

So I checked my xml document again and printed it in a dialog box and it looks fine.

我的想法已经用完,错误可能是......

I'm running out of ideas where the error could be ...

最后,我决定转换xml文档并将其作为字符串发送到服务器。

Finally, I decided to convert the xml document and send it as a string to the server.

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

由于我只需要存储收到的数据,因此我没有任何区别将它作为字符串或xml恢复。

Due to the fact, that I only have to store the recieved data, it makes no difference if I'm revieving it as string or xml.

我只需要改变我的ajax请求,现在一切正常。

I only had to change my ajax request at everything works fine now.

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