最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Receive XML response from Cross-Domain Ajax request with jQuery - Stack Overflow

programmeradmin2浏览0评论

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

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

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.

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

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

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

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

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.

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

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

Share Improve this question edited Mar 22, 2013 at 16:41 Cito asked Mar 21, 2013 at 15:59 CitoCito 1,7093 gold badges22 silver badges49 bronze badges 2
  • in the jQuery forum there is a thread stating it doesnt work forum-thread i hope it helps (its one year old, but...) – winner_joiner Commented Mar 21, 2013 at 20:34
  • Thanks @winner_joiner! I was already saw it. It is just that I was thinking it was a problem jQuery-related (I mean, jQuery avoiding me to read the response). I've investigated the whole day, and I know now I can't do it, and that it can be don with CORS, but SQS does not have it. S3 does, SQS does not... thanks anyways for your help. – Cito Commented Mar 21, 2013 at 21:14
Add a ment  | 

1 Answer 1

Reset to default 11

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

  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)

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

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

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

Unfortunately I was not able to acplished 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...

发布评论

评论列表(0)

  1. 暂无评论