GET request via AJAX only works by entering the address bar

Asked

Viewed 438 times

0

Good morning,

It’s kind of crazy what’s happening, but I’m making a request via AJAX sending a GET and the return is ok, it’s all right, but this return is a url that after the request should allow access, but it only works if the url I am ordering via AJAX I access in the address bar, I can’t understand which logic has this?

Example:

 $.ajax({
        type : "GET",
        url  : 'http://services.teste.com/'+chaveAcesso,      
        dataType: 'json',
        success: function(retorno) {
            if(retorno.url) {             
                console.log(retorno.url);
            }
        }
    });

The return url is coming but it should have an access privilege and only works if I request it from the address bar by pasting this address http://services.teste.com/'+keyAccess and giving a enter and not via ajax

  • The return is used to validate if the person has access is this?

  • The return returns a url to generate an access, for example: urlretorno.com.br/novoidacesso and this url urlretorno.com.br/novoidacesso is coming, but it doesn’t work, but if I copy the ajax call url and paste the url return urlretorno.com.br/novoidaccess will work :S, for example go to the url and paste http://services.test.com/keyAccess ai will return urlretorno.com.br/novoidacess working, via ajax not working

2 answers

0


0

As pointed out by @demarchisd, this is probably a CORS problem.

One way to resolve this is to make the request on the server side and then return with AJAX to the desired page.

For example, in PHP vc you can have a script (meuScript.php) that returns the JSON using the function file_get_contents('http://url.desejada.com/').

From there in java script you do:

$.ajax({
    type : "GET",
    url  : 'caminho/do/arquivo/meuScript.php',      
    dataType: 'json',
    success: function(retorno) {
        if(retorno.url) {             
            console.log(retorno.url);
        }
    }
});
  • Thank you so much for the help, I will implement and soon more return

  • I did with file_get_contents and also via Curl for testing and did not solve, I will contact the people of the api and comment on Cors to see if they are blocking access

Browser other questions tagged

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