$resouce.get() - Uncaught SyntaxError: Unexpected token :

4,206 views
Skip to first unread message

ItsLeeOwen

unread,
Apr 6, 2012, 8:10:48 PM4/6/12
to ang...@googlegroups.com
Uncaught SyntaxError: Unexpected token :

I'm able to retrieve data from our remote server using JSONP, and the response data looks fine, but the callback is not executed, and the above error, referencing services line1 is logged in the console.

{"success":true,"message":null,"data":[{"id":1,"title":"New Episode","events":[],"links":{"AkScene":[{"id":"3","parentClass":"AkEpisode","parentId":"1","childClass":"AkScene","childId":"1"}]},"scalixObject":"AkEpisode","label":"Episode","Tile":null}],"format":"json"}

Any ideas?

Nolan Dubeau

unread,
Apr 6, 2012, 8:12:35 PM4/6/12
to ang...@googlegroups.com
I'm getting this same error and don't know how to get around it.  My JSON is fine as well.

Were you able to find out the issue?

Thanks.

Nolan

Misko Hevery

unread,
Apr 7, 2012, 12:38:13 AM4/7/12
to ang...@googlegroups.com
You are most likely trying to consume json as jsonp. The two are not the same. The server needs to provide jsonp not json

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/WKQRoldgIHAJ.

To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.

Misko Hevery

unread,
Apr 7, 2012, 12:38:48 AM4/7/12
to ang...@googlegroups.com
Or if it is the same server as where the page is hosted then you should use mothed:'get' not method:'json'

ItsLeeOwen

unread,
Apr 7, 2012, 1:38:26 AM4/7/12
to ang...@googlegroups.com
Thanks Misko, that was the case.

Ben McFerren

unread,
Aug 31, 2013, 12:46:22 PM8/31/13
to ang...@googlegroups.com
I was experiencing the same error as a result of this bit of code:

    var dataService = $resource('http://search.ams.usda.gov/farmersmarkets/v1/data.svc/zipSearch?zip=:zip', {callback: 'JSON_CALLBACK'}, {get: {method: 'JSONP'}});
    $scope.domainData = dataService.get({zip: '60613'});

It was producing this url:


when I needed this url


The & instead of the ? made a difference when the code executed the request.

It turned out that I needed to upgrade my js files for angular and resource to the latest versions with these commands: 

bower install -save angular
bower install -save angular-resource

The original yo angular command doesn't bring down the most recent version of these files so the update was required in order to make this JSONP work correctly for me.

After the upgrade, the character was changed in my url string and it was able to make the request. The next challenge I have on my list is to investigate requirejs to see if i can automate this process upon build.

I wanted to post here so that anyone who may be having similar issues can possibly find a solution.

Thanx Mishko for creating this framework. I have enjoyed learning it so far and feel that you have done an excellent job explaining its intricacies.

-Ben

@mcferren

Christiaan Scheerm

unread,
Jan 31, 2014, 5:45:47 AM1/31/14
to ang...@googlegroups.com
Hi Ben,

You could also dynamically add the 'zip' parameter and value to the resource params arguments like so:

var dataService = $resource('http://search.ams.usda.gov/farmersmarkets/v1/data.svc/zipSearch', {callback: 'JSON_CALLBACK'}, {get: {method: 'JSONP'}});
    $scope.domainData = dataService.get({zip: '60613'});
 
Because the ':zip' is not defined in the url angular will automatically convert it into a query parameter.


Christiaan
Reply all
Reply to author
Forward
0 new messages