Skip to content Skip to sidebar Skip to footer

Sending XML Through AJAX

I create a xml document in jQuery as following var xmlDocument = $(''); var foo = $(''); var bar = $(''); foo.append(bar); xmlDocument.append(f

Solution 1:

EDIT:

You have a typo - it's not precessData it's processData

$.ajax({
   url             :   'js/foobar.php',
   type            :   'POST',
   precessData     :   false, // change to processData

and again in sucess which should be success


Try:

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

Solution 2:

You are using jQuery Object through the entire process.

Write your XML like this, concatenating the string together. Not making them as DOM Object.

var xmlDocument = '<xml/>';
xmlDocument += '<foo/>';
xmlDocument += '<bar/>';

Then post it, like this

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

Solution 3:

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

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

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.

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

Solution 4:

I think you have a bug on your code on success

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

Solution 5:

use $.parseXML to manipulate XML , you are treating the xml as if it is html

http://api.jquery.com/jQuery.parseXML/


Post a Comment for "Sending XML Through AJAX"