I'm trying to send a JSON object with a response in a 403 event, that contains token expiration info, amongst others, to the client. However, for some reason the response is sent as a string object instead of JSON.
The response looks like this:

Here are the relevant functions.
This is from the server:
function checkYourPrivilege(checkAdmin, bearerHeader, res, reg, next){
if (typeof bearerHeader !== 'undefined') {
var bearer = bearerHeader.split(" ");
bearerToken = bearer[1];
jwt.verify(bearerToken, secret, function(err, decoded) {
console.log(decoded);
if(err){
return res.json({ success:false, message: 'Failed to authenticate token'});
}else{
if(Math.floor(Date.now() / 1000 < decoded.expires)){
if(checkAdmin){
if(decoded.privileges.admin){
console.log("Tokenisi vanhentuu" + Math.abs(decoded.expires - Math.floo$
reg.decoded = decoded;
next();
}else{
console.log("et ole admin");
return res.status(403).send({
success: false,
tokenstatus: "valid",
message: "Not admin"
});
}
}else{
console.log("Tokenisi vanhentuu" + Math.abs(decoded.expires - Math.floor(Date.n$
reg.decoded = decoded;
next();
}
}else{
console.log("Token Expired");
return res.status(403).send({
success: false,
tokenstatus: "valid",
message: "Not admin"
});
}
}
});
} else {
console.log('ei tokenia');
return res.status(403).send({
success: false,
message: 'No token provided.'
});
}
};
This is from the client:
.factory('authInterceptorService', ['$q', '$location', '$localStorage', function($q, $location, $localStorage){
return {
'request': function (config){
console.log(config);
config.headers = config.headers || {};
if ($localStorage.accessToken) {
config.headers.Authorization = 'bearer ' + $localStorage.accessToken;
}
return config;
},
'responseError': function(response){
if(response.status === 401 || response.status === 403) {
console.log(response);
$location.path('/');
}
return $q.reject(response);
}
};
}])
JSON.parse()to turn it back into a object on the client side. For the record, there's no such thing as a JSON object. JSON is a notation standard, not a object type. So standard javascript objects can be written as a string formatted as JSON.