ajax获取错误消息

时间:2018-04-09 15:09:24

标签: jquery html ajax

我有ajax方法。我发了一个请求。如果收到错误,则会被error: function(data) {

捕获

如何获取收到的错误消息?前面我的谷歌浏览器,网络我可以看到收到的消息:

如何提取'message元素?

从后端,我抛出错误消息,如

throw new Exception("There is already a user registered with the email provided");

在Chrome中我看到:

error:"Internal Server Error"
exception:"java.lang.Exception"
message:"There is already a user registered with the email provided"
path:"/registerAccountant"
status:500
timestamp:1523286158171

如何提取错误消息?

以下是我发送和接收的代码

$.ajax({
        type: accountantRegistrationForm.attr('method'),
        url: accountantRegistrationForm.attr('action'),
        data: fd ,
        contentType: false,
        processData: false,
        success: function(data) {

            $('#accountantSubmitBtn').attr('disabled', false);
            //resetJournalEntryTable();

            setTimeout(function(){
                $.toast({
                    text: 'New Accountant successfully saved.!',
                    showHideTransition: 'fade',
                    hideAfter: 2000,
                    bgColor: '#3b78e7',
                    textColor: '#fff',
                    allowToastClose: false,
                    position: 'top-center',
                    icon: 'success',
                    loader:false
                });

                $('body').loading('stop');
                /*$('html, body').scrollTop(0);*/
            }, 500);

            //$('#jedatatable').DataTable().ajax.reload();
        },
        error: function(data) {
            setTimeout(function(){
                $.toast({
                    text: 'Accountant Registration failed.1 !',
                    showHideTransition: 'fade',
                    hideAfter: 2000,
                    bgColor: '#3b78e7',
                    textColor: '#fff',
                    allowToastClose: false,
                    position: 'top-center',
                    icon: 'error',
                    loader:false
                });

                $('body').loading('stop');
                /*$('html, body').scrollTop(0);*/
            }, 500);
            $('#accountantSubmitBtn').attr('disabled', false);
            $('body').loading('stop');
        },
    });

1 个答案:

答案 0 :(得分:0)

尝试这样的操作,它会将错误消息抛出到控制台屏幕。

error: function (xmlHttpRequest, textStatus, errorThrown) {
        console.log(errorThrown)
    }