External Ajax URL Request

Asked

Viewed 939 times

0

I am with following problem,I implemented the code,to get information from RSS blog,however I am not able to access url criticizing error 401,when I place local file works normally.

var ajax = new XMLHttpRequest();

    ajax.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            console.log("Dados" + ajax.responseText);
            startComunicado(this);
        }
    };
    ajax.open("GET", "http://www.infomoney.com.br/blogs/investimentos/off-the-records/rss", true);
    ajax.send();
};

2 answers

0

Do it this way

                  $.ajax({
                    type: 'GET',
                    crossDomain: true,
                    data: form,
                    dataType: 'json',
                    url: 'http://www.infomoney.com.br/blogs/investimentos/off-the-records/rss',


                    success: function (dados) {
                        if (dados != null) {

                           //fazer o que quiser
                        }

                    },
                    error: function () {
                        console.log("Falha de enviar AJAX");
                    }
                })

inserir a descrição da imagem aqui

  • I implemented the following error "ailed to load http://www.infomoney.com.br/blogs/investiments/off-the-records/rss: No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'http://localhost' is therefore not allowed access. ajaxvs1.html:28 Failed to send AJAX"

  • I’ll edit the answer, there’s a few things missing

  • Now appears following error; "Uncaught Referenceerror: form is not defined"

  • just take out of the data:form... you are not sending anything

  • It worked my dear?

  • No.Shows the same error as before "Failed to load http://www.infomoney.com.br/blogs/investiments/off-the-records/rss: No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'http://localhost' is therefore not allowed access."

  • You already have somewhere to lodge it?

  • My return here was right

  • If your problem has been solved please check as Solved. It may be useful for others with the same problem

Show 4 more comments

0

The mistake Status Code 401 happens when you make a request, but you are not allowed.

Already the error cited in the comment of the previous reply:

"Failed to load infomoney.com.br/blogs/investimentos/off-the-records/rss: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost'; is therefore not allowed access."

It happens due to lack of header Access-Control-Allow-Origin on the server response.

To get around this, you can use the site https://cors.io/. It will serve as an intermediary and will automatically add the header necessary.

Example with Fetch:

fetch("https://cors.io/?http://www.infomoney.com.br/blogs/investimentos/off-the-records/rss")
    .then( resp => resp.text() )
    .then(resp => console.log(resp))
    .catch( err => console.debug(err) );

Example with XHR:

var ajax = new XMLHttpRequest();

ajax.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        console.log(ajax.responseText);
    }
};
ajax.open("GET", "https://cors.io/?http://www.infomoney.com.br/blogs/investimentos/off-the-records/rss", true);
ajax.send();
  • Thanks for the Overview,about crossdomain,everything ok,but when I try to access it, follow http://blog.linkcom.com.br/plus/ has error 500?

  • The mistake 500 is internal server problem (other errors you can see in https:/httpstatuses.com). When accessing this site, I received a box to inform user and password, however, as I do not have access, I received the error 401 (as I explained in the reply).

Browser other questions tagged

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