通过jQuery AJAX传递XML的不同方法
我遇到了获取返回值的问题(content-type:“text / xml”)。 通过直接访问此URL,我可以获得返回值:
https://[domain_name]/myfolder/myapi/?xml=
如果错误(在MyFolder
中的HTML中调用),请帮助我纠正这些替代方案,因为它总是警告“失败”。
$.ajax({ type : "GET", url : "interface/?xml=", dataType : "text/xml", success : function(msg){ alert('Success'); } error : function(msg) { alert('Failed'); } });
要么…
$.ajax({ type : "POST", url : "interface/", data : { xml: escape("") }, dataType : "text/xml", success : function(msg){ alert('Success'); } error : function(msg) { alert('Failed'); } });
谢谢。
解
必须通过https
访问该接口,因此我将url
param更改为绝对URL。 我还必须使用"xml"
而不是"text/xml"
作为其dataType
。 结果成功,谢谢。
为简化起见,我会做以下事情
让我们假设你使用的是一个名为script.php的php脚本。
var xml_string = " "; $.get('script.php', {xml: xml_string}, function(){ //your success function alert('success'); }).error(function(){ //your error function alert("error"); });
这会接受POST吗…从你的例子看,它看起来像是GET的设置..试试这个:
$.ajax({ type : "GET", url : "http://blachblahblah.com/abc.html", dataType : "text/xml", data : { xml : escape(" ") }, success : function(msg){ alert('Success'); } , error : function(msg) { alert('Failed'); } });
我不明白你为什么使用dataType?
你想要/需要的是contentType。
来自api.jquery.com:
dataType(默认值:Intelligent Guess(xml,json,script或html))类型:String您希望从服务器返回的数据类型。 如果没有指定,jQuery将尝试根据响应的MIME类型推断它………
contentType(默认值:’application / x-www-form-urlencoded; charset = UTF-8’)类型:字符串将数据发送到服务器时,请使用此内容类型。 默认为“application / x-www-form-urlencoded; charset = UTF-8”,这在大多数情况下都适用。 如果您明确地将内容类型传递给$ .ajax()………….
希望这可以提供帮助