【问题标题】:How to return error message from ajax post in Node.js如何从 Node.js 中的 ajax 帖子返回错误消息
【发布时间】:2016-08-27 06:18:38
【问题描述】:

我正在通过 ajax 向节点 js 服务器提交一个表单,如果有任何错误,我预计会出现错误,但我没有向我显示错误,而是被重定向到一个带有错误的全新页面。我用过 res.send , res.json, res.writheHead()... 但我总是重定向到一个新页面

            $.ajax({
                url: $this.attr('/api/adduser'),
                type: $this.attr('POST'),
                data: $this.serialize(),
                dataType: 'json', // JSON
                success: function(json) {
                     alert('Erreur : '+ json.reponse);
                    }
                })
            event.preventDefault()

在服务器端我有:

    sql.query("INSERT into internes(email,nom,prenom,password,privilege,datenaissance,gender,details,user_Add_Mail)"+
            " VALUES(lower($1),lower($2),lower($3),lower($4),lower($5),$6,$7,lower($8),lower($9))",
            req.body.email,req.body.nom,req.body.prenom,req.body.pass1,priv,req.body.datenaissance,parseInt('0'),req.body.details,req.body.passAdmin)
            .then(function(result){
                res.redirect('/api/users');
            })
            .catch(function(erreur){
                res.json(400, {'success': erreur})
            })

【问题讨论】:

  • res.status(400).json({err:"custom"});

标签: ajax node.js express


【解决方案1】:

您遇到的错误似乎被识别为您的 sql 承诺的成功响应。要解决这个问题,请执行类似

的操作
sql.query("INSERT into internes(email,nom,prenom,password,privilege,datenaissance,gender,details,user_Add_Mail)"+
        " VALUES(lower($1),lower($2),lower($3),lower($4),lower($5),$6,$7,lower($8),lower($9))",
        req.body.email,req.body.nom,req.body.prenom,req.body.pass1,priv,req.body.datenaissance,parseInt('0'),req.body.details,req.body.passAdmin)
        .then(function(result){
           // look into your result to see if you have what you asked for
           if(result.error) {
                res.status(500).send({error: 'you have an error'}); 
           }
           res.redirect('/api/users');
        })
        .catch(function(erreur){
            res.json(400, {'success': erreur})
        })

【讨论】:

    【解决方案2】:

    一种选择是使用 ajax 错误:

    success: function (json) {
        alert(json);
    },
    error: function (jqXHR, exception) {
        var msg = '';
        if (jqXHR.status === 0) {
            msg = 'Not connect.\n Verify Network.';
        } else if (jqXHR.status == 404) {
            msg = 'Requested page not found. [404]';
        } else if (jqXHR.status == 500) {
            msg = 'Internal Server Error [500].';
        } else if (exception === 'parsererror') {
            msg = 'Requested JSON parse failed.';
        } else if (exception === 'timeout') {
            msg = 'Time out error.';
        } else if (exception === 'abort') {
            msg = 'Ajax request aborted.';
        } else {
            msg = 'Uncaught Error.\n' + jqXHR.responseText;
        }
        alert(msg);
    }
    

    Doc

    【讨论】:

      猜你喜欢
      • 2014-05-14
      • 2014-09-03
      • 2020-11-30
      • 1970-01-01
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-04
      相关资源
      最近更新 更多