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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:55:03  来源:igfitidea点击:

HTTP DELETE verb in Node.JS

javascriptnode.jshttprestexpress

提问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, POSTor PUTrequests, but DELETErequests won't work.

我可以做GETPOSTPUT请求,但DELETE请求将无法正常工作。

DELETE http://localhost:8081/api/1.0/entryyields undefinedfrom 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();
});