0

I'm starting to learn Angular and I want to play around with the $http call function. I basically want to make a get request to an external API:

angular.module('pokedexApp')
  .controller('MainCtrl', function ($scope, $http) {
    var response;
    $http.jsonp("http://pokeapi.co/api/v1/pokemon/1/").success(function(response) {
        response = response;
    })

    $scope.pokemon = response;
  });

I'm probably completely wrong as to how this works. I basically want to {{ pokemon }} out a response to test it.

I'm getting a Uncaught SyntaxError: Unexpected token : error and I can't tell if it's because the return data isn't properly formatted or what.

2 Answers 2

1

The error is because in angular it will try to parse this response in json type. And it failed to parse, so it throw an error:

Uncaught SyntaxError: Unexpected token :        

Seems in angularjs you'd better to handler jsonp response by your self.

  $scope.triggerJsonp = function(){
      $http.jsonp("http://pokeapi.co/api/v1/pokemon/1/", {params: {'callback':'eqfeed_callback'}});  
  };
  window.eqfeed_callback  = function(data){
      $scope.response = data;
  }

Here is jsfiddle.

Relative issue in jquery.ajax JSONP call showing "Uncaught SyntaxError: Unexpected token : " And answer use own callback to handle this.

Sign up to request clarification or add additional context in comments.

1 Comment

Interesting. Thanks for showing the relative issue too. Clears things up.
0

It doesn't look like the website provides JSONP support - https://github.com/phalt/pokeapi_issues/issues/30

Your $http request is fine but you currently have a scoping issue with respect to 'response'. This is how your code should be written in order for your scope variable to be defined correctly:

angular.module('pokedexApp')
   .controller('MainCtrl', function ($scope, $http) {
        $http.jsonp("http://pokeapi.co/api/v1/pokemon/1/").success(function(response) {
            $scope.pokemon = response;
        })    
   });

4 Comments

I think you are wrong. I just try $http.jsonp("pokeapi.co/api/v1/pokemon/1/") in chrome. And get a success response data from it.
I think the success response can be tricky/misleading. Are you able to actually get data from it?
Nice catch on the callback!
Thx. Here is another relative issue in jquery. JSONP call showing “Uncaught SyntaxError: Unexpected token : ”

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.