javascript - AngularJS Uncaught SyntaxError: Unexpected token : - Stack Overflow

Okay, iI tried several attempts and nothing is workingChecked the following answersquestions26262235j

Okay, iI tried several attempts and nothing is working

Checked the following answers

  1. questions/26262235/jsonp-returning-uncaught-syntaxerror-unexpected-token-angularjs-routingnu

  2. questions/16344933/angularjs-jsonp-not-working/16352746#16352746

  3. questions/19269153/jsonp-request-in-angularjs-doesnt-work-like-in-jquery

  4. questions/19669044/angularjs-getting-syntax-error-in-returned-json-from-http-jsonp

And non of them solved my problem.

I would like to use Giant Bombs API: /

Yes i took a look at the forum posts nothing works.

$http({
        method: 'JSONP',
        url: '/',
        params: {
            api_key: $rootScope.api_key,
            format: 'jsonp',
            callback: 'JSON_CALLBACK'
        }
    }).then(function (data) {
        $scope.data = data;
        console.log($scope.data)
    });

Error

Uncaught SyntaxError: Unexpected token :

Could someone give me a hint?

Because its really frustrating, I even wrapped the data with JSON_CALLBACK() same result

Okay, iI tried several attempts and nothing is working

Checked the following answers

  1. questions/26262235/jsonp-returning-uncaught-syntaxerror-unexpected-token-angularjs-routingnu

  2. questions/16344933/angularjs-jsonp-not-working/16352746#16352746

  3. questions/19269153/jsonp-request-in-angularjs-doesnt-work-like-in-jquery

  4. questions/19669044/angularjs-getting-syntax-error-in-returned-json-from-http-jsonp

And non of them solved my problem.

I would like to use Giant Bombs API: http://www.giantbomb./api/

Yes i took a look at the forum posts nothing works.

$http({
        method: 'JSONP',
        url: 'http://www.giantbomb./api/game/3030-4725/',
        params: {
            api_key: $rootScope.api_key,
            format: 'jsonp',
            callback: 'JSON_CALLBACK'
        }
    }).then(function (data) {
        $scope.data = data;
        console.log($scope.data)
    });

Error

Uncaught SyntaxError: Unexpected token :

Could someone give me a hint?

Because its really frustrating, I even wrapped the data with JSON_CALLBACK() same result

Share Improve this question asked Oct 31, 2015 at 12:02 user1130272user1130272 2
  • Can you add plete code where the error is, you can get hint from the console – Tushar Commented Oct 31, 2015 at 12:08
  • I suppose You checked if the code itself doesn't have the SyntaxError. So the "unexpected colon" will be in the response. Jsonp is javascript object encapsulate into the calling of function. Please provide code of response. – Víťa Plšek - angular.cz Commented Oct 31, 2015 at 12:15
Add a ment  | 

1 Answer 1

Reset to default 1

The reason of this is, that angular requires service returns jsonp, but it doesn't

Your code does this request:

http://www.giantbomb./api/game/3030-4725/?json_callback=angular.callbacks._0&format=jsonp

And in network console you can see the response:

{"error":"'jsonp' format requires a 'json_callback' arguement","limit":0,"offset":0,"number_of_page_results":0,"number_of_total_results":0,"status_code":103,"results":[]}

so the parameter for callback should be named: json_callback, not just callback.

$http({
        method: 'JSONP',
        url: 'http://www.giantbomb./api/game/3030-4725/',
        params: {
            format: 'jsonp',
            json_callback: 'JSON_CALLBACK'
        }
    })

After that I can see in response error about api key - this will probably be ok in your instance. So I uppose you will get correct JSONP

http://www.giantbomb./api/game/3030-4725/?json_callback=angular.callbacks._0&format=jsonp

{"error":"Invalid API Key","limit":0,"offset":0,"number_of_page_results":0,"number_of_total_results":0,"status_code":100,"results":[]}

The other problem would be that your function in then doesn't get data directly, but response, which is objects which carry data, so:

.then(function (response) {
        $scope.data = response.data;
        console.log(response.data)
    });

The last one remendation, do not use $scope in controllers, rather use "controller as" syntax.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745376938a4625043.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信