通过jQuery AJAX传递XML的不同方法

Jea*_*ert 6 xml ajax jquery

我遇到了一个获得返回值的问题(content-type:"text/xml").通过直接访问此URL,我可以获得返回值:

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

如果错误(在HTML中调用),请帮助我纠正这些替代方案,MyFolder因为它始终提醒"失败".

$.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');
    }
});
Run Code Online (Sandbox Code Playgroud)

要么...

$.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');
    }
});
Run Code Online (Sandbox Code Playgroud)

谢谢.

必须访问该接口https,因此我将urlparam 更改为绝对URL.我也必须使用"xml""text/xml"作为它dataType.结果成功,谢谢.

IM.*_*IM. 5

这会接受POST吗...从你的例子看,它看起来像是GET的设置..试试这个:

$.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'); } 
}); 
Run Code Online (Sandbox Code Playgroud)


Kyl*_*yle 3

为了简化,我会执行以下操作

假设您正在使用名为 script.php 的 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");
});
Run Code Online (Sandbox Code Playgroud)