使用 jQuery 从跨域 Ajax 请求接收 XML 响应 [英] Receive XML response from Cross-Domain Ajax request with jQuery

查看:45
本文介绍了使用 jQuery 从跨域 Ajax 请求接收 XML 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图向另一个域发出 ajax 请求,它已经可以工作了,但现在我遇到了另一个问题...

I trying to make an ajax request to another domain, it already works, but now I have another problem...

这是我的代码:

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

正如我所说,它已经工作,但问题是服务器返回一个 XML(不是我的服务器,是另一家公司的另一台服务器 - 一个网络服务 - 所以我不能改变它返回的内容)和 jsonp 所期望的一个 json 失败并出现错误:

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:

SyntaxError: syntax error
<?xml version="1.0"?><ReceiveMessageResponse xmlns="http://q ... />

根据 jQuery 文档,添加 jsonp text xml 应该很神奇,将响应转换为简单文本,然后将其解析为 XML,但它不起作用.

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.

我已经能够使用 YQL 实现它,但它的限制是每小时 10,000 个请求,而我正在开发的系统每小时最多将有 1000 万个请求.出于同样的原因,我无法在自己的服务器中代理"这些请求...

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...

仅供参考:我正在尝试从 SQS 获取最新消息,所以如果无论如何要告诉它以 json 形式返回数据,它会更容易和更好,但我在文档中也没有找到任何内容...

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...

推荐答案

我的问题的简单答案是:只有两种方法可以做到这一点:

The plain answer to my question is this: There are only two ways of do this:

  1. 使用代理.我不会把所有的制作方法放在这里,但你可以在网上找到很多信息,搜索cors"跨域ajax请求"和yql"(最后一个是雅虎的代理)

  1. 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)

使用 CORS.这是跨源资源共享.即:激活要从中获取信息的服务器,以将信息发送到任何其他域并响应来自任何其他域的请求.为此,您必须是管理服务器/服务的人.

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.

这两种方法是从另一个域获取信息 XML(或任何其他格式)的唯一方法.进行json跨域请求:

Those two are the only ways of getting information XML (or any other format) from another domain. To make json cross domain requests:

  • 使用 jsonp(Json 填充).我不会解释这个(实际上只是额外的信息,因为如果来自服务器的答案是 XML,它就无法工作——我的主要问题),导致 很多 网络信息.

不幸的是,我没能实现我的目标,因为 SQS 没有配置为任何这些方法......不过,我对跨域请求的工作原理有很多了解.我希望这可以帮助任何人...

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...

这篇关于使用 jQuery 从跨域 Ajax 请求接收 XML 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆