我试图从客户端到服务器端发布一个动作号,并从服务器接收响应,但我确实得到:
angular.js:8467 POST SITE:3000/posts/test 404 (Not Found)
我的代码有什么问题?
服务器端index.js
router.post('/posts/test', auth, function(req, res, next) {
res.json("success");
});
angularjs客户端文件(post service下):
o.crimePerform = function(post) {
return $http.post('/posts/test', post, {
headers: {Authorization: 'Bearer '+auth.getToken()}
}).success(function(data){
console.log(data);
console.log("performed crime!");
});
}
angularjs客户端文件(实际执行的操作)
$scope.performacrime = function(action) {
console.log("performing action number " + action.id);
posts.crimePerform({crimeaction : action.id});
$scope.status = action.name + ' loading...';
$scope.crimeresult = true;
}
响应:angular.js:8467 POST WEBSITE:3000/posts/test 404 (Not Found)
EDIT:服务器的响应只是一个例子。
你的问题似乎不是与你的角POST。它在服务器端。您的服务器端代码出错。
我试过了,例如:
curl -H "Content-Type: application/json" -X POST -d '{"Authorization":"Bearer xyz"}' http://178.62.5.64:3000/posts/test
结果是:
<h1>Not Found</h1>
<h2>404</h2>
<pre>Error: Not Found
at /opt/mean/app.js:48:15
at Layer.handle [as handle_request] (/opt/mean/node_modules/express/lib/rout
er/layer.js:82:5)
at trim_prefix (/opt/mean/node_modules/express/lib/router/index.js:270:13)
at /opt/mean/node_modules/express/lib/router/index.js:237:9
at Function.proto.process_params (/opt/mean/node_modules/express/lib/router/
index.js:312:12)
at /opt/mean/node_modules/express/lib/router/index.js:228:12
at Function.match_layer (/opt/mean/node_modules/express/lib/router/index.js:
295:3)
at next (/opt/mean/node_modules/express/lib/router/index.js:189:10)
at /opt/mean/node_modules/express/lib/router/index.js:191:16
at Function.match_layer (/opt/mean/node_modules/express/lib/router/index.js:
295:3)</pre>
您的服务器端路由正在被调用,并且看起来错误处理正在给出404响应。