为什么我的 angular/express GET 有效但 POST 无效?
Why does my angular/express GET work but not POST?
这里是我的按钮的设置方式。 Updates.getUpdates
正在工作。 Updates.postAnUpdate
returns 404
$scope.postUpdate = function () {
console.log($scope.update);
Updates.postAnUpdate($scope.update);
Updates.getUpdates().then(function (data) {
$scope.updates = data;
});
};
这是我可爱的服务
app.factory('Updates', ['$http',
function ($http) {
return {
//Get the current users messages
getUpdates: function () {
return $http({
url: '/updates/',
method: 'get'
}).then(function (result) {
return result.data;
});
},
postAnUpdate: function (update) {
return $http({
url: '/updates/post',
method: 'post',
data: {
update:update,
}
}).then(function (result) {
return result.data;
});
}
};
}]);
这是我处理 url 的路径
var updates = require('./routes/updates.js');
//Project Updates
app.get('/updates/', updates.getAll);
app.get('/updates/post', updates.newPost);
最后,这是使用 200 和控制台文本的代码。
exports.getAll = function (req, res) {
console.log('It worked');
}
所以一切都应该适用于 post,但事实并非如此。我只是想执行一个控制台命令,所以我知道它有效并且我得到了 404
exports.newPost = function (req, res) {
var db = mongo.db,
BSON = mongo.BSON,
newPost = {};
console.log('This is giving me 404 instead of showing up in terminal');
newPost.content = req.body.update;
newPost.author = req.user._id;
newPost.date = new Date();
db.collection('updates').save(newPost, function (err, result) {
if (err) {
throw err;
}
console.log(result);
});
}
看起来这是一个简单的印刷错误。在你的路线中:
app.get('/updates/', updates.getAll);
app.get('/updates/post', updates.newPost);
我想你想要
app.post('/updates/post', updates.newPost);
这里是我的按钮的设置方式。 Updates.getUpdates
正在工作。 Updates.postAnUpdate
returns 404
$scope.postUpdate = function () {
console.log($scope.update);
Updates.postAnUpdate($scope.update);
Updates.getUpdates().then(function (data) {
$scope.updates = data;
});
};
这是我可爱的服务
app.factory('Updates', ['$http',
function ($http) {
return {
//Get the current users messages
getUpdates: function () {
return $http({
url: '/updates/',
method: 'get'
}).then(function (result) {
return result.data;
});
},
postAnUpdate: function (update) {
return $http({
url: '/updates/post',
method: 'post',
data: {
update:update,
}
}).then(function (result) {
return result.data;
});
}
};
}]);
这是我处理 url 的路径
var updates = require('./routes/updates.js');
//Project Updates
app.get('/updates/', updates.getAll);
app.get('/updates/post', updates.newPost);
最后,这是使用 200 和控制台文本的代码。
exports.getAll = function (req, res) {
console.log('It worked');
}
所以一切都应该适用于 post,但事实并非如此。我只是想执行一个控制台命令,所以我知道它有效并且我得到了 404
exports.newPost = function (req, res) {
var db = mongo.db,
BSON = mongo.BSON,
newPost = {};
console.log('This is giving me 404 instead of showing up in terminal');
newPost.content = req.body.update;
newPost.author = req.user._id;
newPost.date = new Date();
db.collection('updates').save(newPost, function (err, result) {
if (err) {
throw err;
}
console.log(result);
});
}
看起来这是一个简单的印刷错误。在你的路线中:
app.get('/updates/', updates.getAll);
app.get('/updates/post', updates.newPost);
我想你想要
app.post('/updates/post', updates.newPost);