Javascript Nodejs响应作为字符串而不是JSON发送

Javascript Nodejs响应作为字符串而不是JSON发送,javascript,node.js,http,Javascript,Node.js,Http,我正在尝试向客户端发送一个JSON对象,其中包含403事件中的响应,其中包括令牌过期信息。但是,由于某些原因,响应将作为字符串对象而不是JSON发送 响应如下所示: 以下是相关功能 这来自服务器: function checkYourPrivilege(checkAdmin, bearerHeader, res, reg, next){ if (typeof bearerHeader !== 'undefined') { var bearer =

我正在尝试向客户端发送一个JSON对象,其中包含403事件中的响应,其中包括令牌过期信息。但是,由于某些原因,响应将作为字符串对象而不是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.'
                });
        }
};
试用
res.status(403).json({})
代替
res.status(403).发送({})

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

JSON是一个字符串。JSON在机器之间始终作为字符串发送。只需使用
JSON.parse()
将其转换回客户端的对象。作为记录,没有JSON对象这样的东西。JSON是一种符号标准,而不是对象类型。所以标准的javascript对象可以写成JSON格式的字符串。在爬回我的洞穴之前,我会向全世界道歉。-@nyoatype你仍然应该按照维生素的建议去做。如果您使用res.json,它将发送具有正确内容类型的数据,AngularJS将自动为您解析数据。没有理由使用JSON.parse()。JSON是字符串。JSON始终作为字符串在机器之间发送。只需使用
JSON.parse()
将其转换回客户端的对象。作为记录,没有JSON对象这样的东西。JSON是一种符号标准,而不是对象类型。所以标准的javascript对象可以写成JSON格式的字符串。在爬回我的洞穴之前,我会向全世界道歉。-@nyoatype你仍然应该按照维生素的建议去做。如果您使用res.json,它将发送具有正确内容类型的数据,AngularJS将自动为您解析数据。没有理由使用JSON.parse()。
.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);
                }
        };
}])