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

查看:170
本文介绍了使用jQuery从Cross-Domain 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(不是我的服务器,是来自另一家公司的另一台服务器 - 一个Web服务 - 因此我无法更改它返回的内容)并且因为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(这最后是Yahoo的代理)

  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 Padded)。我不会解释这个(实际上只是额外的信息,因为如果服务器的答案是XML - 我的主要问题它将无法工作),原因 很多 网上信息

  • 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 is a lot of information on the web.

不幸的是我无法完成我的目标,因为SQS没有配置任何这种方法......但是,我对Cross-Domains Requests的工作方式有了充分的了解。我希望这对任何人都有帮助......

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从Cross-Domain Ajax请求接收XML响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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