javascript Node.JS 中的 HTTP DELETE 动词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14173770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTTP DELETE verb in Node.JS
提问by bevacqua
Do I need to set up any configuration before I can make DELETE requests to a node.js application?
在向 node.js 应用程序发出 DELETE 请求之前,是否需要设置任何配置?
I can make GET
, POST
or PUT
requests, but DELETE
requests won't work.
我可以做GET
,POST
或PUT
请求,但DELETE
请求将无法正常工作。
DELETE http://localhost:8081/api/1.0/entry
yields undefined
from the routing logger, I'm using express to register the routes. But it looks like I can't even resolve the url / verb.
DELETE http://localhost:8081/api/1.0/entry
收益率undefined
从路由记录器,我使用的快递注册路线。但看起来我什至无法解析 url/动词。
This is how I'm invoking it:
这就是我调用它的方式:
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();
});
});
Sample log
示例日志
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
采纳答案by hunterloftis
Hopefully this can help:
希望这可以帮助:
Enable logging as your first middleware to make sure the request is coming in:
app.use(express.logger());
Use the methodOverride() middleware:
app.use(express.bodyParser());
app.use(express.methodOverride()); // looks for DELETE verbs in hidden fields
Create a .del() route:
app.del('/api/1.0/entry', function(req, res, next) { ... });
启用日志记录作为您的第一个中间件以确保请求进入:
app.use(express.logger());
使用 methodOverride() 中间件:
app.use(express.bodyParser());
app.use(express.methodOverride()); // looks for DELETE verbs in hidden fields
创建一个 .del() 路由:
app.del('/api/1.0/entry', function(req, res, next) { ... });
回答by morphy
PUT and DELETE values for typesetting are not supported by all browsers:
并非所有浏览器都支持类型设置的PUT 和 DELETE 值:
See documentation http://api.jquery.com/jQuery.ajax/
请参阅文档http://api.jquery.com/jQuery.ajax/
You can use POST type including data:{_method:'delete'}in ajax request:
您可以在 ajax 请求中使用包含data:{_method:'delete'} 的POST 类型:
$.ajax({
data:{_method:'delete'},
url: '/api/1.0/entry',
type: 'POST'
}).done(function(res){
var row = $(this).parentsUntil('tbody');
row.slideUp();
});