How to get JSON from an external url

Asked

Viewed 1,501 times

-1

I’m trying to catch a JSON from an external page, I’m using the dataType=jsonp, but only that the url, returns me a JSON and the following error:

Uncaught Syntaxerror: Unexpected token :

The JSON returns me the following token:

{"token":"WLAR-i0ad819840000014fcceb5726da5fd20a"}

and I my code is 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!'); },                                                                                                                               
});

2 answers

1

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

dataType: 'jsonp'

To:

dataType: 'json'

EDIT

You can still "circumvent" these Crossdomain exceptions using a proxy CORS, such as the http://cors.io/ or the http://crossorigin.me/

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!'); },                                                                                                                               
});
  • to access different domains I cannot use the dataType: 'json' , browsers do not allow this type of request

  • What you can do to access different domains like this is to use a CORS proxy, like http://cors.io/ or http://crossorigin.me/

  • Leave the dataType as 'jsonp' in this case is wrong, jQuery will wait for the call of a function

  • but when using the 'json'it returns the following error: crossDomain (default: false for same-domain requests, true for cross-domain requests) Type: Boolean If you wish to force a 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)

  • And if you use the json enabling crossDomain how true? I said this when I edited my answer above. If it helps take into consideration to take that -1 of the answer I gave.

  • Do with the example @Rafaeltelles added, tested here and worked with CORS.IO

  • If we use Cors or crossorigin, it returns success, but the data that comes as success is an error message "{"error":{"errors":["incorrect_userid"],"localizedErrorMessages":["incorrect_userid"],"firstError":"incorrect_userid"}}

  • @Rafaelcabral, you are not passing data along with the request

  • The requisition is being made successfully, if there is any other problem, I suggest you ask another question, not to pollute this

Show 4 more comments

0

From what I observed in your code there is a comma (,) right after the closure of the success in your call AJAX, in the code below 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 leave the comma is to call another function or attribute from jQuery AJAX, for example, error, complete, follows the example showing that you returned an error message right after the function declaration 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 function error did not put a comma at the end of the function, this serves for declarations of more attributes or functions that may come last in the AJAX declaration.

EDIT

Based on Sergio’s comment in my answer that the commas do not influence I decided to put the code in Jsfiddle and test, really the problem persisted, so I did 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 I removed the jsonp and put in place the json only, but now is returning this error saying that external access is not allowed:

Xmlhttprequest cannot load http://www.revistadostribunais.com.br/maf/api/v1/authenticate.json?sp=ESDEVA-1. No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access.

EDIT

To enable AJAX to access externally, state 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 a 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 your AJAX call:

$.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!'); }                                                                                                                               
});
  • Whether or not a forgotten comma makes no difference: http://jsfiddle.net/8jucf87s/

  • Exactly what Sergio said, the comma makes no difference :|

  • the "two points" returned as error comes from the {"token":"WLAR-i0ad819840000014fcceb5726da5fd20a"}

  • Thanks @Sergio for your comment, I edited the post. Try now by changing the jsonp for json, I believe will return now another problem regarding external access. If you find the solution to this problem I will edit the post again.

  • Yes, change to JSON is not possible, browsers do not allow this type of request

  • Try declaring this in your AJAX call: crossDomain: true,

  • What was the result? What did you do to make it work?

  • For now I haven’t been able to make it work with jquery with any of the options, what I got was PHP, but my application is C#, so it didn’t do much, with C# tbm I haven’t been able to get it... but this is a question for another topic...

  • I’ll get more on the Internet, and then put my solution here!

Show 4 more comments

Browser other questions tagged

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