[英]MEAN stack/angularjs post request failure
Im struggeling to get the server to accept my post request, cause i would like to post some data on /api/kill . 我正在努力让服务器接受我的发布请求,因为我想在/ api / kill上发布一些数据。
For some reason it wont work. 由于某种原因,它将无法正常工作。 how can i make it work?
我该如何运作?
I downloaded the latest release for Mean stack by this link: https://github.com/dickeyxxx/mean-sample 我通过以下链接下载了Mean Stack的最新版本: https : //github.com/dickeyxxx/mean-sample
i tried to edit the article module to start with. 我试图编辑文章模块开始。
var promise = $http.put('/api/kill', null);
response: 响应:
angular.js:11756 PUT SITE:3000/server-error 404 (Not Found)
attempt number two: 尝试第二:
var promise = $http.post('/api/kill', null);
returns: 返回:
angular.js:11756 POST SITE:3000/api/kill 404 (Not Found)
( only if the route is: (仅当路线为:
app.route('/api/kill').all(articlesPolicy.isAllowed)
.get(articles.list)
.put(articles.killuserbyid)
so, if route is : 因此,如果route是:
app.route('/api/kill').all(articlesPolicy.isAllowed)
.get(articles.list)
.post(articles.killuserbyid)
it would return the orginal: 它会返回原始的:
angular.js:11756 PUT SITE:3000/server-error 404 (Not Found)
MY codes: 我的代码:
in my article.server.routes.js i do have: 在我的article.server.routes.js中,我确实有:
'use strict';
/**
* Module dependencies
*/
var articlesPolicy = require('../policies/articles.server.policy'),
articles = require('../controllers/articles.server.controller');
module.exports = function (app) {
// Articles collection routes
app.route('/api/kill').all(articlesPolicy.isAllowed)
.get(articles.list)
.put(articles.killuserbyid)
.delete(articles.delete);
// Single article routes
// Finish by binding the article middleware
app.param('articleId', articles.articleByID);
};
my invokeRolesPolicies
in articles.server.policy: 我在
invokeRolesPolicies
中的invokeRolesPolicies
:
exports.invokeRolesPolicies = function () {
acl.allow([{
roles: ['admin'],
allows: [{
resources: '/api/kill',
permissions: '*'
}]
}, {
roles: ['user'],
allows: [{
resources: '/api/kill',
permissions: '*'
}]
}, {
roles: ['guest'],
allows: [{
resources: '/api/kill',
permissions: '*'
}]
}]);
};
my ArticlesListController
function in list-articles.client.controller: 我在list-articles.client.controller中的
ArticlesListController
函数:
ArticlesListController.$inject = ['ArticlesService','$scope'];
function ArticlesListController(ArticlesService,$scope) {
var vm = this;
vm.testing = function() {
ArticlesService.test();
}
}
and, finally my articleservice function (articles.client.service) 最后是我的articleservice函数(articles.client.service)
ArticlesService.$inject = ['$resource','$http'];
function ArticlesService($resource,$http) {
var o = [];
o.test = function() {
console.log("tester..");
var promise = $http.put('/api/kill', null);
promise.then(function(payload) {
console.log(payload.data);
});
}
return o;
}
If I am understanding your question/issue correctly, you are receiving a 404 error, when issuing a POST request to /api/kill
in article.server.routes.js
如果我正确理解了您的问题/问题,则在对
article.server.routes.js
/api/kill
发出POST请求时收到404错误
Correct me if I'm wrong, but your current route in your code is: 如果我错了,请纠正我,但是您当前在代码中的路线是:
app.route('/api/kill').all(articlesPolicy.isAllowed)
.get(articles.list)
.put(articles.killuserbyid)
.delete(articles.delete);
As it stands, there are only route handlers for GET
PUT
and DELETE
requests. 就目前而言,只有用于
GET
PUT
和DELETE
请求的路由处理程序。 There are no route handlers for POST
requests, so your server is issuing a 404 error since it cannot find a route handler to respond to POST
requests to /api/kill
. 没有
POST
请求的路由处理程序,因此您的服务器发出404错误,因为它找不到路由处理程序来响应对/api/kill
POST
请求。
Try modifying your route to something like the below and see if you get a response 尝试将您的路线修改为如下所示,然后查看是否得到响应
app.route('/api/kill').all(articlesPolicy.isAllowed)
.get(articles.list)
.post(...) // replace ... with appropriate route handling function
.put(articles.killuserbyid)
.delete(articles.delete);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.