Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught SyntaxError: Unexpected token : in AngularJS

Tags:

angularjs

I've been working on this for a few hours and can't seem to get anywhere. I hope this is the right place to post this. I'm new to StackOverflow so please guide me to the right place if this isn't correct.

I have the following html file:

<!DOCTYPE HTML>
<html ng-app="DishClips">
 <head>
    <script type="text/javascript" src="http://code.angularjs.org/angular-1.0.0.0rc4.min.js"></script>
    <script type="text/javascript" src="http://code.angularjs.org/angular-resource-1.0.0.0rc4.min.js"></script>
    <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.0/css/bootstrap.min.css">
    <script type="text/javascript" src="./dishclips.js"></script>
 </head>

 <body>
 <div ng-controller="DishClipsCtrl">
    <table class="table table-striped">
        <tr ng-repeat="restaurant in dishclipsResult.results"></tr>
            <td>{{restaurant.name}}</td>
    </table>
 </div>
 </body>
</html>

And the following in my dishclips.js:

angular.module('DishClips', ['ngResource']);

function DishClipsCtrl($scope, $resource) {
    $scope.dishclips = $resource('http://apiv2.dishclips.com/dishclips/api/:action',
    {action:'searchRestaurants',address:'irvine',callback:'JSON_CALLBACK' },
    {get:{method:'JSONP'}});

    $scope.dishclipsResult = $scope.dishclips.get(); 
}

When I run this (in chrome) I get:

Uncaught SyntaxError: Unexpected token :

The json return looks great so I don't understand why this is an issue.

Thanks

like image 768
Tan Rezaei Avatar asked Sep 30 '13 22:09

Tan Rezaei


2 Answers

Because the API you used returns JSON instead of JSONP. The JSONP's payload should be wrapped with a function like this functionCall({"Name": "Foo", "Id": 1234, "Rank": 7});

like image 137
zs2020 Avatar answered Nov 15 '22 01:11

zs2020


You must define one more default param in the JSONP request.

get:{method:'JSONP', params : {callback : 'JSON_CALLBACK'}}
like image 3
daron_ Avatar answered Nov 14 '22 23:11

daron_