How to get JSON from an external url

-1
I'm trying to get a JSON from an external page, I'm using dataType=jsonp , but only that url, returns me a JSON and the following error: / p>
  

Uncaught SyntaxError: Unexpected token:

The JSON returns me the following token:

{"token":"WLAR-i0ad819840000014fcceb5726da5fd20a"}

And I have my code like this:

$.ajax({
    type: 'GET',
    url: 'http://www.revistadostribunais.com.br/maf/api/v1/authenticate.json?sp=ESDEVA-1', 
    dataType: 'jsonp',
    success: function() { console.log('Success!'); },                                                                                                                               
});
    
asked by anonymous 14.09.2015 / 19:57

2 answers

1

The problem is that the request does not return the call of a method and you want to interpret the response as JSONP, you must simply change

dataType: 'jsonp'

To:

dataType: 'json'

EDIT

You can still "circumvent" these CrossDomain exceptions by using a CORS proxy, such as the link or the link

The code would look something like:

$.ajax({
    type: 'GET',
    url: 'http://cors.io/?u=http://www.revistadostribunais.com.br/maf/api/v1/authenticate.json?sp=ESDEVA-1', 
    dataType: 'json',
    success: function() { console.log('Success!'); },                                                                                                                               
});

Or ...

$.ajax({
    type: 'GET',
    url: 'http://crossorigin.me/http://www.revistadostribunais.com.br/maf/api/v1/authenticate.json?sp=ESDEVA-1', 
    dataType: 'json',
    success: function() { console.log('Success!'); },                                                                                                                               
});
    
14.09.2015 / 20:17
0

I noticed in your code that there is a comma ( , ) shortly after closing success in your AJAX call, in the code below I removed the comma, note:

$.ajax({
    type: 'GET',
    url: 'http://www.revistadostribunais.com.br/maf/api/v1/authenticate.json?sp=ESDEVA-1',
    dataType: 'jsonp',
    success: function() { console.log('Success!'); }
});

What you could do to drop the comma is to call another jQuery AJAX function or attribute, such as error , complete , follow the example showing that you returned an error message shortly after the declaration of function sucess :

$.ajax({
    type: 'GET',
    url: 'http://www.revistadostribunais.com.br/maf/api/v1/authenticate.json?sp=ESDEVA-1',
    dataType: 'jsonp',
    success: function() {    
        console.log('Success!'); 
    }, error: function(e){
         alert('Ocorreu um erro durante a chamada ' + e);
    }
});

Note that after closing the error function, I did not put a comma at the end of the function, this is used for declarations of the most attributes or functions that may come last in the AJAX declaration.

EDIT

Based on Sergio's comment in my reply that the commas do not influence I decided to put the code in JSFiddle and test, the problem really persisted, so I made the following code snippet:

$.ajax({
    type: 'GET',
    url: 'http://www.revistadostribunais.com.br/maf/api/v1/authenticate.json?sp=ESDEVA-1',
    dataType: 'json',
    success: function() { console.log('Success!'); }

});

Notice that I removed the jsonp and put the json in place only, but is now returning this error saying that external access is not allowed:

  

XMLHttpRequest can not load link . In 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' link ' is therefore not allowed access.

EDIT

To enable AJAX to access externally, declare this in your AJAX:

 crossDomain: true,

Documentation:

  

CrossDomain (default: false for same-domain requests, true for cross-domain requests)   Type: Boolean   If you wish to force crossDomain request (such as JSONP) on the same domain, set the value of crossDomain to true. This allows, for example, server-side redirection to another domain. (version added: 1.5)

EDIT

Example of what your AJAX call would look like:

$.ajax({
    type: 'GET',
    url: 'http://www.revistadostribunais.com.br/maf/api/v1/authenticate.json?sp=ESDEVA-1', 
    crossDomain: true,
    dataType: 'json',
    success: function() { console.log('Success!'); }                                                                                                                               
});
    
14.09.2015 / 20:06