Node.JS中的HTTP DELETE动词
在我可以向一个node.js应用程序发出DELETE请求之前,是否需要设置任何configuration?
我可以做GET
, POST
或者PUT
请求,但是DELETE
请求不起作用。
DELETE http://localhost:8081/api/1.0/entry
从路由logging器中得到undefined
,我使用express来注册路由。 但它看起来像我甚至无法解决的url/动词。
这是我如何调用它:
rows.find('a.remove').on('click', function(){ $.ajax({ url: '/api/1.0/entry', type: 'DELETE' }).done(function(res){ var row = $(this).parentsUntil('tbody'); row.slideUp(); }); });
示例日志
GET / 200 18ms GET /author/entry 200 10ms GET /api/1.0/entry 200 2ms GET /api/1.0/entry 200 1ms GET /api/1.0/entry 200 1ms undefined
希望这可以帮助:
-
启用日志logging作为您的第一个中间件,以确保请求进入:
app.use(express.logger());
-
使用methodOverride()中间件:
app.use(express.bodyParser());
app.use(express.methodOverride()); // looks for DELETE verbs in hidden fields
-
创build一个.del()路线:
app.del('/api/1.0/entry', function(req, res, next) { ... });
所有浏览器都不支持types设置的PUT和DELETE值:
请参阅文档http://api.jquery.com/jQuery.ajax/
您可以在ajax请求中使用POSTtypes,包括数据:{_ method:'delete'} :
$.ajax({ data:{_method:'delete'}, url: '/api/1.0/entry', type: 'POST' }).done(function(res){ var row = $(this).parentsUntil('tbody'); row.slideUp(); });