最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Nodejs响应以字符串而不是JSON的形式发送

IT培训 admin 6浏览 0评论

Nodejs响应以字符串而不是JSON的形式发送

我正在尝试在403事件中发送带有响应的JSON对象,其中包含令牌到期信息,以及其他信息到客户端。但是,由于某种原因,响应将作为字符串对象而不是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);
                }
        };
}])
回答如下:

尝试使用res.status(403).json({})而不是res.status(403).send({})

Nodejs响应以字符串而不是JSON的形式发送

我正在尝试在403事件中发送带有响应的JSON对象,其中包含令牌到期信息,以及其他信息到客户端。但是,由于某种原因,响应将作为字符串对象而不是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);
                }
        };
}])
回答如下:

尝试使用res.status(403).json({})而不是res.status(403).send({})

发布评论

评论列表 (0)

  1. 暂无评论