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');
}
});
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?
An ajax request without a xml document works fine and I get the ‘foo’ back.
UPDATE #1
After changing precessData to processData and sucess to success i get the failed to send ajax request
dialog.
When I change the data parameter in the ajax method to
$.ajax({
...
data : {
data: xmlDocument
},
...
});
I also get the failed to send ajax request
dialog.
The code on the server side should be fine cause it’s only
<?php
echo 'foo';
?>
UPDATE #2
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);
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);
Still the same behaviour.
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 …