Load and read XML via AJAX Cross-Domain

Asked

Viewed 1,041 times

1

I have an AJAX Cross-Domain request that receives an XML. After a lot of work, I was able to download the XML but I don’t know if I did it the right way because it never arrives in Success. Follow 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 downloading XML, it is shown in the Google Chrome console the following error message speaking XML: Uncaught Syntaxerror: Unexpected token < in row 1 (header).

  • View your browser’s Network, and see the status of this request whether it is 200, 304, 403, 404 or 500

  • @Tafarelchicotti but I don’t want to treat the mistake, I want it not to have to be wrong.

  • @Tafarelchicotti status 200. I edited the question and put an error message that Google Chrome displays referring to XML.

  • Tell me which line is going wrong?

  • @Tafarelchicotti line 1. Logo on the header.

  • where you put jsonp, mute to xml

  • @Tafarelchicotti there the cross-Omain doesn’t work. Gives permission error: "Xmlhttprequest cannot load http://finansite-a.ae.com.br/localiza/xml/localiza.xml. No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'http://localhost:3064' is therefore not allowed access".

  • poem as html the return and uses the var $xml = $(data).parseXML(); and try to see if it goes

Show 3 more comments

1 answer

2


What happens is that you have determined the data type as JSONP, this means that the result of request that you are doing will return a JSON that will be executed in a callback defined by you, i.e., assuming that the url you are calling returns the following JSON: {"Name": "Foo", "Id" : 1234, "Rank": 7}, in the case of JSONP you should add to the end of the URL that you are making the request ?jsonp=callback, and then the request would take the result JSON and pass as parameter to the function callback as an 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 function of callback, thus, when jQuery tries to parse the answer to pass parameter to its callback it cannot, because it tries to parse a JSON but is actually an XML.

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

You have several options of backend to perform this type of processing: in PHP you have the lib CURL in Python you have the urllib2 in Ruby you have the class Net::Httprequest

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

  • Thanks! I managed to solve this way!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.