5

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

2
  • use the development version of angular instead of the minified version and then tell your browser to break before exception. The stack trace might have useful information. Commented Sep 30, 2013 at 23:07
  • did you resolved it? Commented Jan 21, 2015 at 14:57

3 Answers 3

3

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});

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

4 Comments

Was just typing out this exact answer but you beat me to it.
I'm not sure exactly what this means? What part of the code do I need to wrap in the fanctionCall?
@sza can you help me figure out where this function needs to go? I am not sure I understand the hint on how to fix this. thank you
The server-side API needs to wrap the JSON result in a function call, that's how JSONP works. Since I'm guessing you don't have control over this API, your best bet is to create an API on the server which serves your web content. This API should just forward requests to the other API. This will circumvent the same-origin issues.
3

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

get:{method:'JSONP', params : {callback : 'JSON_CALLBACK'}}

Comments

0

You also get this error if accidentally load the template page (instead or on top of the directive .js file) as a script.

<script src="/js/Directives/Templates/myTemplate.html"></script>

Comments

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.