最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论