P
P
paulvales2016-04-04 12:00:46
JavaScript
paulvales, 2016-04-04 12:00:46

Why doesn't want to parse JSON?

There is some API, you need to get a token.
I receive data:

function gettok2(){
    var result;
 $.ajax({
 type: 'GET', 
     cache: false,
   callbackParameter: 'callback',
    async: false,
       crossDomain: true,
    dataType: 'jsonp',
  url: "https://api.shipper.io/v1/login/key",
  data:{ username:"xxxxxx",auth_key:"xxxxxxxxxxxxxxxxxxxx"},
  success: function(data){
    var r = $.parseJSON(data);
    result = r.access_token; 
     console.log(r.access_token);
  alert(r);
  }
});
 return result;
}

The data comes in like this:
{"access_token":"gmZxervvZWL-qop3vS6d8qTwjamBk5QM"}

But in the console "Uncaught SyntaxError: Unexpected token :"
Points to a colon.
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry Eremin, 2016-04-04
@EreminD

try

...
success: function(data){
    var r = $.parseJSON(data);
    result = r["access_token"]; 
     console.log(r["access_token"]);
  alert(r);
...

P
Peter, 2016-04-04
@petermzg

Your situation is similar to this
one . the problem is in the cross-site policies

N
Nicholas, 2016-04-04
@Chuv

Are you using jsonp? This means that in the success callback, data is already a normal js object and does not need to be parsed.

S
SirMustache, 2016-04-04
@SirMustache

Try adding to the request url:
?callback=JSON_CALLBACK

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question