javascript 使用 jQuery 从跨域 Ajax 请求接收 XML 响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15552375/
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
Receive XML response from Cross-Domain Ajax request with jQuery
提问by Cito
I trying to make an ajax request to another domain, it already works, but now I have another problem...
我试图向另一个域发出 ajax 请求,它已经可以工作了,但现在我有另一个问题......
This is my code:
这是我的代码:
function getChannelMessages(channel) {
jQuery.support.cors = true;
$.ajax(channel, {
cache : true,
type : "get",
data : _channels[channel].request,
global : false,
dataType : "jsonp text xml",
jsonp : false,
success : function jsonpCallback (response) {
console.log(response);
updateChannelRequest(channel);
//getChannelMessages(channel);
}
});
}
As I said, it already works, but the problem is the server returns an XML (Is not my server, is another server from another company - a web service - so I can not change what it returns) and as jsonp expects an json it fails with the error:
正如我所说,它已经工作了,但问题是服务器返回一个 XML(不是我的服务器,是另一家公司的另一台服务器 - 一个网络服务 - 所以我不能改变它返回的内容)并且因为 jsonp 需要一个 json失败并出现错误:
SyntaxError: syntax error
<?xml version="1.0"?><ReceiveMessageResponse xmlns="http://q ... />
According to jQuery documentation, adding jsonp text xml
should make the magic, converting the response to simple text and then parsing it as XML, but it does not works.
根据 jQuery 文档,添加jsonp text xml
应该很神奇,将响应转换为简单文本,然后将其解析为 XML,但它不起作用。
I was already able to make it using YQL, but it has a limit of 10,000 requests per hour, and the system I'm developing will have up to 10 million request per hour. For that same reason, I can not "proxy" in my own server those requests...
我已经能够使用 YQL 来实现它,但它的限制是每小时 10,000 个请求,而我正在开发的系统每小时最多将有 1000 万个请求。出于同样的原因,我无法在我自己的服务器中“代理”这些请求......
FYI: I'm trying to get the newest messages from SQS, so if there is anyway to tell it to return the data as json, it will be easier and better, but I have not find anything either in the documentation...
仅供参考:我正在尝试从 SQS 获取最新消息,所以如果无论如何要告诉它以 json 形式返回数据,它会更容易更好,但我在文档中也没有找到任何内容......
回答by Cito
The plain answer to my question is this: There are only two ways of do this:
我的问题的简单答案是:只有两种方法可以做到这一点:
Use a proxy. I won't put here all the how-to's to make it, but you can find a lot of information in the web searching for "cors" "cross domains ajax requests" and "yql" (this last is a proxy by Yahoo)
Use CORS. This is Cross-Origin Resource Sharing. That is: activate the server from which you want to get information to sent information to any other domain and to answer to requests from any other domain. To do this you must be the one who manage the server/service.
使用代理。我不会把所有的制作方法放在这里,但你可以在网上找到很多信息,搜索“cors”“跨域ajax请求”和“yql”(最后一个是雅虎的代理)
使用 CORS。这就是跨域资源共享。即:激活要从中获取信息的服务器,以将信息发送到任何其他域并响应来自任何其他域的请求。为此,您必须是管理服务器/服务的人。
Those two are the only ways of getting information XML (or any other format) from another domain. To make json cross domain requests:
这两种是从另一个域获取信息 XML(或任何其他格式)的唯一方法。进行json跨域请求:
- Use jsonp (Json Padded). I won't explain this (and actually is just extra information since it won't work if the answer from the server is XML - my main problem), cause there isa lot ofinformation on the web.
Unfortunately I was not able to accomplished my goal, cause SQS is not configured to any of this methods... Still, I've got plenty insight of how Cross-Domains Requests works. And I hope this help anyone...
不幸的是,我无法实现我的目标,因为 SQS 没有配置为任何这些方法......不过,我对跨域请求的工作原理有很多了解。我希望这可以帮助任何人......