通过 jQuery AJAX 传递 XML 的不同方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5176548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Different ways to pass XML via jQuery AJAX
提问by Jeaf Gilbert
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:
我在获取返回值时遇到问题(内容类型:“text/xml”)。我可以通过直接访问这个 URL 来获得返回值:
https://[domain_name]/myfolder/myapi/?xml=<xml version='1.0'><MyTasks><Search></Search></MyTasks>
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'.
如果这些选项有误(在位于 中的 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');
}
});
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.
该接口必须由 访问https
,因此我将url
param更改为绝对 URL。我也必须使用"xml"
not"text/xml"
作为它的dataType
. 结果成功,谢谢。
采纳答案by Kyle
To simplify, I would do the following
为简化起见,我将执行以下操作
Lets assume you are using a php script called script.php.
让我们假设您正在使用一个名为 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");
});
回答by IM.
Does this take POSTs at all .. from your example, it looks like its setup for GETs.. Try this :
这是否完全需要 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'); }
});
回答by hop
I don't understand why you use the dataType ?
我不明白你为什么使用 dataType ?
What you want/need is contentType.
您想要/需要的是 contentType。
From api.jquery.com :
从 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.........
dataType(默认值:Intelligent Guess(xml、json、script 或 html)) 类型:String 您期望从服务器返回的数据类型。如果没有指定,jQuery 将尝试根据响应的 MIME 类型推断它.......
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().............
contentType(默认值:'application/x-www-form-urlencoded; charset=UTF-8') 类型:String 向服务器发送数据时,使用此内容类型。默认为“application/x-www-form-urlencoded; charset=UTF-8”,这在大多数情况下都适用。如果您显式地将内容类型传递给 $.ajax() .....................
Hope this can help
希望这可以帮助