Nodejs响应以字符串而不是JSON

时间:2017-12-28 10:47:31

标签: javascript node.js http

我正在尝试在403事件中发送带有响应的JSON对象,其中包含令牌到期信息,以及其他信息到客户端。但是,由于某种原因,响应将作为字符串对象而不是JSON发送。

响应如下:

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

1 个答案:

答案 0 :(得分:2)

尝试使用 res.status(403).json({})代替 res.status(403).send({})