Load and read XML via AJAX Cross-Domain

1

I have a Cross-Domain AJAX request that receives an XML. After a lot of work, I was able to download the XML but I do not know if I did it correctly because it never arrives at success . Here is the requisition code:

function refreshPage() {

$.ajax({ url: 'http://finansite-a.ae.com.br/localiza/xml/localiza.xml',
    crossDomain: true, 
    dataType: 'jsonp',
    success: function (response) {
        $(response).find('li a').each(function () {
            listHref.push($(this).attr('href'));
        });

        var nome_arquivos = new Array();
        var DHTML = (document.getElementById || document.all || document.layers);
        var xmlDocWin;
        var ini_coluna_win;
        function pegarDiv(nome) {
            if (document.getElementById) {
                this.obj = document.getElementById(nome);
            }
            else if (document.all) {
                this.obj = document.all[nome];
            }
            else if (document.layers) {
                this.obj = document.layers[nome];
            }
        }

        function escrever_campo(id, texto) {
            if (!DHTML) return;
            var x = new pegarDiv(id);
            if (x.obj) {
                x.obj.innerHTML = texto;
            }
            return;
        }

        function pegaCor(valor) {
            cor = 'semvar';
            temp = valor.replace(",", ".");
            if (parseFloat(temp) == 0)
                cor = 'semvar';
            else if (parseFloat(temp) > 0)
                cor = 'varpos';
            else if (parseFloat(temp) < 0)
                cor = 'varneg';
            return cor;
        }

    }

});}

When you download the XML, the following XML error message is displayed in the Chrome console: Uncaught SyntaxError: Unexpected token on line 1 (header).

    
asked by anonymous 09.05.2014 / 21:51

1 answer

2

What happens is that you have determined the data type as JSONP, that means that the result of the request you are doing will return a JSON that will run in a callback defined by you, ie assuming that the url you are calling returns the following JSON: {"Name": "Foo", "Id" : 1234, "Rank": 7} , in the case of JSONP you should append to the end of the URL that you are doing the ?jsonp=callback request, and then the request would fetch the result JSON and would pass as parameter to the callback function as for example: callback({"Name": "Foo", "Id" : 1234, "Rank": 7});

The first and main problem is that your request is not returning a JSON but an XML, and the second problem is that you are not passing any callback function, so when jQuery tries to parse the response to pass parameter to its callback function it fails, as it tries to parse a JSON but is actually an XML.

I would recommend you do this with a backend language instead of trying to use JS.

You have several backend options to do this kind of processing: in PHP you have the CURL in Python you have urllib2 in Ruby you have the class Net :: HTTPRequest

Just like in other languages you have their respective libraries that make HTTP Requests in addition to libraries to handle XML, which will make it easier for you to parse the answer.

    
09.05.2014 / 23:11