0
클라이언트에서 토큰 만료 정보를 포함하는 403 이벤트에서 응답이있는 JSON 객체를 보내려고합니다. 그러나 어떤 이유로 응답은 JSON 대신 문자열 객체로 전송됩니다.Nodejs 응답이 JSON 대신 문자열로 전송됩니다.
응답은 다음과 같습니다
이 서버에서이다 :
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.'
});
}
};
이 클라이언트에서이다 :
.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 문자열입니다. – Quentin
JSON은 항상 컴퓨터간에 문자열로 전송됩니다. 그냥 JSON.parse()를 사용하여 클라이언트 측의 객체로 되돌립니다. 레코드에는 JSON 오브젝트와 같은 것이 없습니다. JSON은 객체 유형이 아닌 표기법 표준입니다. 따라서 표준 자바 스크립트 객체는 JSON 형식의 문자열로 작성 될 수 있습니다. – Shilly
신성한 몰리 나는 지금 어리 석다. 내 동굴로 돌아 가기 전에 전 세계에 대해 사과 할 것입니다 .- – nyoatype