AngularJS : 工厂 $http.get 请求继续获取 html 文件作为 return
AngularJS : factory $http.get request keep getting html file as return
我可以知道为什么我总是从我的 angular 工厂得到 html 文件作为 return 吗?
这是我的后台路由
function ensureAuthenticated(req, res, next) {
if (!req.headers.authorization) {
return res.status(401).send({ message: 'Please make sure your request has an Authorization header' });
}
var token = req.headers.authorization.split(' ')[1];
var payload = jwt.decode(token, config.TOKEN_SECRET);
if (payload.exp <= moment().unix()) {
return res.status(401).send({ message: 'Token has expired' });
}
req.user = payload.sub;
next();
}
app.get('/api/me', ensureAuthenticated, userHandler.getMe);
这是从 userHandler 调用的 getMe 函数
this.getMe = function(req, res, next){
// retrieve data from database by using req.user as id
User.findById(req.user, function(err, user) {
res.send(user);
});
}
在我的 service.js( 我已经尝试通过将路由 '/api/me' 更改为其他路由来在此处进行调试,但当不存在此类路由时它仍然 return 状态 200路线在我的后端。
app.factory('Account', function($http) {
return {
getProfile: function() {
return $http.get('/api/me');
},
updateProfile: function(profileData) {
return $http.put('/api/me', profileData);
}
};
});
在我的控制器中
$scope.getProfile = function() {
Account.getProfile()
.success(function(data) {
$scope.user = data;
console.log(data) // this print out the html file
})
.error(function(error) {
alert("something is wrong when retriving your data");
});
};
$scope.getProfile();
console.log(data) 给我这个
在网络选项卡中
谁能帮我解决这个问题?我可以知道有什么方法可以调试这类问题吗?谢谢 !
只需发送 json 作为回应
functionensureAuthenticated(req,res,next){
if(!req.headers.authorization){
return res.json { status :'401',
message: 'PleasemakesureyourrequesthasanAuthorizationheader'
};
}vartoken=req.headers.authorization.split('')[
1
];varpayload=jwt.decode(token,
config.TOKEN_SECRET);if(payload.exp<=moment().unix()){
return res.json{
status: '401',
message: 'Tokenhasexpired'
}
}req.user=payload.sub;next();
最终由我的 nodejs app.js 文件引起,我弄乱了行的顺序。
"use strict";
// all environments
app.use(compress());
app.use(require('prerender-node'));
app.set('port', process.env.PORT || 3000);
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/static', express.static(path.join(__dirname, '/public')));
app.use(favicon());
app.use(errorHandler());
app.enable('trust proxy');
//for html5 mode
app.get('*',function(req, res){ // this
res.sendFile('master.html', { root: path.join(__dirname, '/public/views/') });
});
routes(app, mongoose); // and this
mongoose.connect(config.MONGO_URI);
mongoose.connection.on('error', function() {
console.error('MongoDB Connection Error. Please make sure that MongoDB is running.');
});
http.createServer(app).listen(app.get('port'),function(){
console.log('Express server listening on port ' + app.get('port'));
});
应该是
routes(app, mongoose); // this
//for html5 mode
app.get('*',function(req, res){ // and this
res.sendFile('master.html', { root: path.join(__dirname, '/public/views/') });
});
非常抱歉浪费您的时间。不过,我真的很感激你们为帮助我解决这个问题所做的努力。谢谢!
我可以知道为什么我总是从我的 angular 工厂得到 html 文件作为 return 吗?
这是我的后台路由
function ensureAuthenticated(req, res, next) {
if (!req.headers.authorization) {
return res.status(401).send({ message: 'Please make sure your request has an Authorization header' });
}
var token = req.headers.authorization.split(' ')[1];
var payload = jwt.decode(token, config.TOKEN_SECRET);
if (payload.exp <= moment().unix()) {
return res.status(401).send({ message: 'Token has expired' });
}
req.user = payload.sub;
next();
}
app.get('/api/me', ensureAuthenticated, userHandler.getMe);
这是从 userHandler 调用的 getMe 函数
this.getMe = function(req, res, next){
// retrieve data from database by using req.user as id
User.findById(req.user, function(err, user) {
res.send(user);
});
}
在我的 service.js( 我已经尝试通过将路由 '/api/me' 更改为其他路由来在此处进行调试,但当不存在此类路由时它仍然 return 状态 200路线在我的后端。
app.factory('Account', function($http) {
return {
getProfile: function() {
return $http.get('/api/me');
},
updateProfile: function(profileData) {
return $http.put('/api/me', profileData);
}
};
});
在我的控制器中
$scope.getProfile = function() {
Account.getProfile()
.success(function(data) {
$scope.user = data;
console.log(data) // this print out the html file
})
.error(function(error) {
alert("something is wrong when retriving your data");
});
};
$scope.getProfile();
console.log(data) 给我这个
在网络选项卡中
谁能帮我解决这个问题?我可以知道有什么方法可以调试这类问题吗?谢谢 !
只需发送 json 作为回应
functionensureAuthenticated(req,res,next){
if(!req.headers.authorization){
return res.json { status :'401',
message: 'PleasemakesureyourrequesthasanAuthorizationheader'
};
}vartoken=req.headers.authorization.split('')[
1
];varpayload=jwt.decode(token,
config.TOKEN_SECRET);if(payload.exp<=moment().unix()){
return res.json{
status: '401',
message: 'Tokenhasexpired'
}
}req.user=payload.sub;next();
最终由我的 nodejs app.js 文件引起,我弄乱了行的顺序。
"use strict";
// all environments
app.use(compress());
app.use(require('prerender-node'));
app.set('port', process.env.PORT || 3000);
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/static', express.static(path.join(__dirname, '/public')));
app.use(favicon());
app.use(errorHandler());
app.enable('trust proxy');
//for html5 mode
app.get('*',function(req, res){ // this
res.sendFile('master.html', { root: path.join(__dirname, '/public/views/') });
});
routes(app, mongoose); // and this
mongoose.connect(config.MONGO_URI);
mongoose.connection.on('error', function() {
console.error('MongoDB Connection Error. Please make sure that MongoDB is running.');
});
http.createServer(app).listen(app.get('port'),function(){
console.log('Express server listening on port ' + app.get('port'));
});
应该是
routes(app, mongoose); // this
//for html5 mode
app.get('*',function(req, res){ // and this
res.sendFile('master.html', { root: path.join(__dirname, '/public/views/') });
});
非常抱歉浪费您的时间。不过,我真的很感激你们为帮助我解决这个问题所做的努力。谢谢!