Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receive XML response from Cross-Domain Ajax request with jQuery

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

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

like image 898
Cito Avatar asked Mar 21 '13 15:03

Cito


1 Answers

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

like image 100
Cito Avatar answered Oct 14 '22 07:10

Cito