Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different ways to pass XML via jQuery AJAX

Tags:

jquery

ajax

xml

I'm facing a problem to get return value (content-type: "text/xml"). I'm able to get return value by direct access this URL:

https://[domain_name]/myfolder/myapi/?xml=<xml version='1.0'><MyTasks><Search></Search></MyTasks>

Please help me to correct these alternatives if it is wrong (called in HTML located in MyFolder) because it always alert 'Failed'.

$.ajax({
    type     : "GET",
    url      : "interface/?xml=<xml version='1.0'><MyTasks><Search></Search></MyTasks>",
    dataType : "text/xml",
    success  : function(msg){
        alert('Success');
    }
    error    : function(msg) {
        alert('Failed');
    }
});

or...

$.ajax({
    type     : "POST",
    url      : "interface/",
    data     : { xml: escape("<MyTasks><Search></Search></MyTasks>") },
    dataType : "text/xml",
    success  : function(msg){
        alert('Success');
    }
    error    : function(msg) {
        alert('Failed');
    }
});

Thank you.

SOLUTION

The interface has to be accessed by https, so I changed url param to absolute URL. I also have to use "xml" not "text/xml" as its dataType. It results Success, thank you.

like image 504
Jeaf Gilbert Avatar asked Mar 03 '11 04:03

Jeaf Gilbert


2 Answers

Does this take POSTs at all .. from your example, it looks like its setup for GETs.. Try this :

$.ajax({     
   type     : "GET",
   url      : "http://blachblahblah.com/abc.html",
   dataType : "text/xml",
   data     : { xml : escape("<xml version='1.0'><MyTasks><Search></Search></MyTasks>") },
   success  : function(msg){ alert('Success'); } ,
   error    : function(msg) { alert('Failed'); } 
}); 
like image 90
IM. Avatar answered Oct 16 '22 17:10

IM.


To simplify, I would do the following

Lets assume you are using a php script called script.php.

var xml_string = "<xml version='1.0'><MyTasks><Search></Search></MyTasks>";

$.get('script.php', {xml: xml_string}, function(){ //your success function
  alert('success');
}).error(function(){ //your error function
  alert("error");
});
like image 3
Kyle Avatar answered Oct 16 '22 19:10

Kyle