1

I have a function called login, when the User pushes the button, the code below will be executed.

So i try to see if the user exists and to get his data in an JSON Object but that doesn't work, i get following Message:

  SyntaxError: Unexpected token s
    at Object.parse (native)

the URL is exactly like i want it to be.

$scope.login = function() {
    var request = $http({
        method : "GET",
        url : "/REST/user" + "/" + $scope.email + "/" + "password" + "/" + $scope.password,
        headers : {
            'Content-Type' : "application/json"
        }
        });

    request.success(function(response) {
        location.href = "../views/test.html"
    });
    request.error(function(response) {
        growl.addErrorMessage("doesn't work", {
            ttl : 4000
        });
    });
}

Output by Browser

{
"id": "9be1804a-e366-11e4-9d4b-82ea0d805d53",
"email": "[email protected]",
"facebook_id": null,
"firstname": "test",
"lastname": "blabla",
"password": null,
"gender": null,
"is_active": "0",
"birthday": null
}
7
  • You might want to include the output of the REST url if you visit it from the browser Commented May 1, 2015 at 16:09
  • You can use the $http.get("/REST/user" + "/" + $scope.email + "/" + "password" + "/" + $scope.password) syntax to simplify your request. Commented May 1, 2015 at 16:11
  • Hey Jake, i already tried, but same message. Commented May 1, 2015 at 16:16
  • you tried without the headers? Commented May 1, 2015 at 16:39
  • yes but the effect is the same Commented May 1, 2015 at 16:58

1 Answer 1

2

This error is due to JSON.parse throwing an error when it encounters an object with key that doesn't start with quotes ".

For example:

{ abc: 123 }

would throw "Unexpected token a".

$http, by default, treats any data in response with "Content-Type": "application/json" or anything that starts with { or [ as JSON, and invokes JSON.parse.

Take a look at the response in the Network tab of the developer console to see what may have triggered it.

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

1 Comment

same thing happened to me when the api I called throw error message, the http expects a valid json in return.

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.