GET request via AJAX only works by giving an enter in the address bar

0

Good morning,

It's kind of crazy that's happening, but I'm doing a request via AJAX sending a GET and the return is okay, that's all right, but this return is a url that after making the request should allow access, but only works if the url that I'm requesting via AJAX I access in the address bar, I can not 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 make the request through the address bar by pasting this address link '+ keyAccess and giving an enter and not via ajax

    
asked by anonymous 11.10.2016 / 15:29

2 answers

0

One possibility is that the server you're trying to run AJAX does not accept CORS (Cross-origin resource sharing)

    
11.10.2016 / 17:54
0

As pointed out by @demarchisd, it is probably a CORS issue.

One way to resolve this is to do the request on the server side and then return with AJAX to the page you want.

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

Then in the 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);
        }
    }
});
    
11.10.2016 / 18:20