开发者

Different ways to pass XML via jQuery AJAX

开发者 https://www.devze.com 2023-02-14 07:16 出处:网络
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:

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/开发者_如何学C",
    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.


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


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


I don't understand why you use the dataType ?

What you want/need is contentType.

From api.jquery.com :

dataType (default: Intelligent Guess (xml, json, script, or html)) Type: String The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response.........

contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8') Type: String When sending data to the server, use this content type. Default is "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases. If you explicitly pass in a content-type to $.ajax().............

Hope this can help

0

精彩评论

暂无评论...
验证码 换一张
取 消