I have an AngularJS app that I want to alert or capture the message of the following two returned JSON values, either on success or failure.
Two problems occur. On status code 500 I can not get the Message value of the returned JSON I get undefined
when I try to alert msg
. On success I can not parse the JSON either.
Message in Network console
GET inviteUsers 500 Internal Server Error
{"Message":"Users are not available"}
What do I need to do to make this work?
JSON on failure - returns status code 500
{"Message":"Users are not available"}
JSON on success
{"Message": "Invitations sent successfully"}
Controller Method
$scope.inviteUsers = function(){
var msg = JSON.parse(createNewUserService.inviteUsers().query()["Message"]);
}
Service Method (Method GET)
var _inviteUsers = function(){
return $resource(serviceBase + 'inviteUsers',
{
});
};
In fail condition you can access your message in error callback function
var msg;
var inviteUsers = createNewUserService.inviteUsers().query();
inviteUsers.$promise.then(function(data) {
// success handler
msg = JSON.parse(data)["Message"];
}, function(error) {
// error handler
msg = JSON.parse(error)["Message"];
});