2017-12-28 37 views
0

私は、403イベントで応答があるJSONオブジェクトを送信しようとしています。その中には、とりわけ、トークンの有効期限情報が含まれています。しかし何らかの理由で、応答はJSONの代わりに文字列オブジェクトとして送信されます。JSONの代わりにNodejsレスポンスが文字列として送信されます

応答は次のようになります。ここでは

HTTP Response

は、関連する機能です。

これは、サーバーから次のとおりです。

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); 
       } 
     }; 
}]) 
+2

のJSONが文字列です。 – Quentin

+1

JSONは常にマシン間で文字列として送信されます。 'JSON.parse()'を使ってクライアント側のオブジェクトに戻してください。レコードに関しては、JSONオブジェクトのようなものはありません。 JSONは表記法の標準であり、オブジェクト型ではありません。標準のjavascriptオブジェクトは、JSON形式の文字列として記述できます。 – Shilly

+0

ホーリー・モリー今私は愚かだと感じる。私は洞窟に戻ってくる前に全世界に謝罪します.- – nyoatype

答えて

1

てみ使用 res.status(403).json({})代わり res.status(403).send({})

関連する問題