javascript - 未调用 Ajax 成功/错误但 POST 正在运行

标签 javascript node.js ajax express pug

所以,可能是重复的,但是阅读了一些答案,没有帮助... Node.js 项目、MongoDB、Jade 模板

Jade 中的 Ajax

script.
        $('#add_cmt_btn').click(function (e) {
            $.ajax({
                type: 'POST',
                url: '/comments/add',
                data: $("#add_cmt_form").serialize(),
                success: function () {
                    console.log('success');
                    $('#comment-text').val('');
                    $("#comments-block").load(location.href + " #comments-block>*", "");
                },
                error: function (err) {
                    console.log('error: ' + err);
                }
            });
        })

和形式

form(method='POST', id='add_cmt_form')
      div.row
          div.col-sm-6
              div.form-group-sm
                  label Add comment:
                  textarea.form-control(name='text', cols='50', rows='3')#comment-text
                  input(type='hidden', name='data_id', value=data.id)
                  input(type='hidden', name='data_type', value=data.type)
                  a.btn.btn-sm.btn-dark(id='add_cmt_btn', style='width:20%') Add

在 Node (Express route)中有一个功能,可以添加评论

router.post('/add', auth.ensureAuthenticated, function (req, res) {
    req.checkBody('text', 'Text is required').notEmpty();
    let errors = req.validationErrors();

    if (errors) {
        return console.log(errors);
    } else {
        let comment = new Comment();
        comment.author = req.user.login;
        comment.text = req.body.text;
        comment.type = req.body.data_type;
        comment.data_id = req.body.data_id;

        comment.save(function (err) {
            if (err) {
                console.log(err)
            }
        });
    }
});

当我单击添加到数据库的“添加”按钮评论时,但页面未刷新,需要使用 F5 更新以查看最后的评论。 但是我没有看到任何登录控制台(没有错误,没有成功),textarea 的 val 没有被清空并且 div 没有被重新刷新。 在这种情况下我做错了什么?

最佳答案

您没有结束请求。你不见了res.send() ;在您的快速路线上。

router.post('/add', auth.ensureAuthenticated, function (req, res) {
    req.checkBody('text', 'Text is required').notEmpty();
    let errors = req.validationErrors();

    if (errors) {
        console.log(errors);

        return res.status(500).send('Something bad happened!');

    } else {
        let comment = new Comment();
        comment.author = req.user.login;
        comment.text = req.body.text;
        comment.type = req.body.data_type;
        comment.data_id = req.body.data_id;

        comment.save(function (err) {
            if (err) {
                console.log(err);
                return res.status(500).send(err.message)
            }

            res.send('saved');
        });
    }
});

关于javascript - 未调用 Ajax 成功/错误但 POST 正在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49651335/

相关文章:

node.js - 有人可以建议一个单行正则表达式来解析带有 - 或/分隔符的字母数字和可选数字 ID 吗?

mysql - 如何使用 Express 访问 get 请求的结果(对象)?

javascript - Ajax HTTP 请求 url 不正确

javascript - 错误: AJAX call within AJAX response

javascript - 使用 javascript 在 Photoshop 中查找特定图层的图层组

javascript - 将空字符串返回到 ES6 模板字符串中

javascript - 使用 firebase 隐藏第三方 API key

node.js - 多次调用集群事件消息

javascript - Ajax - 如何在成功函数中使用返回的数组

javascript - 如何使用嵌套 HTML <input[name =""]> 作为 JavaScript 对象/数组键