0

I'm trying to catch response status of GET call but it's doesn't work

$http({
            url: conf.url + 'page/' + $rootScope.name,
            method: "GET"
        })
            .then(function(response) { //200-299 status works correctly
             $rootScope.passwrd = response.data.password;                
             location.href = '#/page';
            },
            function(response) { //500 status: response = SyntaxError: Unexpected token F at Object.parse (native) at fromJson
                if (response.status == 500) {  //response.status = underfined
                    alert('Server error')
                } else {
                    alert('Other error');
                }
            });
2
  • 1
    can you post the json you return from the server? The problem is there Commented Jan 12, 2016 at 14:07
  • json contain only text: "Failed to generate phrase" Error in console: GET blalbalba/page/name 500 (Internal Server Error) Commented Jan 12, 2016 at 14:10

2 Answers 2

1

It seems that your server is sending invalid JSON as said in your comment which is making angularjs parsing crash when he tries to read the result from the server and parse it from json to javascript object.

// response = SyntaxError: Unexpected token F at Object.parse (native) at fromJson

Check the JSON send by your server (copy/paste it on jsonlint.com to validate it).

If it's not JSON, then check that the Content-type of your response is not json.

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

Comments

1

you have to return a valid json object
meaning:

{ "message" : "Failed to generate phrase" }

instead of just the string "Failed to generate phrase"

4 Comments

I'm already trying debug it, but value of response.message is "Unexpected token F"
what lang/framework you use server side? Can you post the relevant code from the server where you return the error?
a json cannot contain just a string like that's why you have a parse error. Meaning the problem lies server side
Yes, json was invalid. Thank you so much!

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.