0

我在我的应用程序和前端使用 AJAX

$.post('/post', $("#submitform").serialize())
                .done(function(res) {
                    //3. Receive the server response, no need to emit an event
                    if (res.success) {
                        //4. Show the updated text
                        console.log('success');
                    }
                    else {
                        alert(res.error);
                    }})
                .fail(function(res) {
                    alert("Server Error: " + res.status + " " + res.statusText);
                });



        return false;
});

我从我的 Node.Js/Express 应用程序路由发回:

res.send(statement);

但是,res.success并没有被触发,而是我进入,alert(res.error)尽管该过程在后端执行良好。

我究竟做错了什么?我应该从我的应用程序的后端发送其他东西吗,比如res.success

谢谢!

4

1 回答 1

1

Since, you are using ExpressJS with NodeJS on your server, you can send a error status code with the server when the HTTP request is not correct:

res.status(400).send('Bad Request')

In your client script which use the jQuery Deferred Object:

So you should use as your code:

$.post('/post', $("#submitform").serialize())
    .done(function(res) {
        // Receive the successful server response
        console.log('success');
    })
    .fail(function(res) {
        // Receive the error server response
        alert("Error: " + res.status + " " + res.statusText);
    });
于 2015-04-13T11:15:35.533 回答