javascript - Node.JS 中的 HTTP DELETE 动词

标签 javascript node.js http rest express

在向 node.js 应用程序发出 DELETE 请求之前,我是否需要设置任何配置?

我可以发出 GETPOSTPUT 请求,但 DELETE 请求不起作用。

DELETE http://localhost:8081/api/1.0/entry 从路由记录器中产生 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

最佳答案

希望这能有所帮助:

  • 启用日志记录作为您的第一个中间件,以确保请求进入:

    app.use(express.logger());

  • 使用 methodOverride() 中间件:

    app.use(express.bodyParser());

    app.use(express.methodOverride());//在隐藏字段中查找 DELETE 动词

  • 创建一个 .del() 路由:

    app.del('/api/1.0/entry', function(req, res, next) { ... });

关于javascript - Node.JS 中的 HTTP DELETE 动词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14173770/

相关文章:

javascript - Node crypto.randomBytes 从函数返回 token

android - 如何通过我的 Android 应用程序对网络服务的用户进行身份验证?

node.js - Sequelize - 查询模型和相关模型 - 或

node.js - 如何在 sails js 中使用存储过程调用?

javascript - 是否可以从 angularjs 中的 Controller 调用特定函数?

google-app-engine - 我可以拥有的最大 "POST"大小是多少?

c# - 使用 HTTPWebRequest 获取远程页面的标题

javascript - 使用 RequireJS 加载 Backbone 和 Underscore

javascript - 嵌入 : DiscordAPIError: Cannot send an empty message

javascript - 清除剪贴板以禁止未经授权的复制,插入消息?