簡體   English   中英

來自spring控制器的ajax中的自定義錯誤消息

[英]custom error message in ajax from spring controller

我有一個控制器,它返回一些對象,在壞的情況下,它應該將錯誤消息傳輸到ajax中的錯誤部分。 responseText不被接受,因為tomcat返回了一個帶有標簽和完整堆棧跟蹤的頁面,但是我只需要幾個單詞就可以向用戶顯示此消息。

    @RequestMapping(value = {"/charts"}, method = RequestMethod.POST)
    @ResponseBody
    public Chart handleSelectionFiled(@RequestBody Chart chart) {

        try{
            chart.doSmth();
        } catch (Exception e) {
            //some code to transmit exception message - this is my question    
        }
        return chart;
     }

Ajax功能

$.ajax({
        contentType: 'application/json',
        mimeType: 'application/json',
        type: frm.attr('method'),
        url: frm.attr('action'),
        dataType: 'json',
        data: data["chosenFile"],
        success: function (response) {
            turnOffPreLoader($("#defects_info"),response);
            $('#selectionForm').submit();

        },
        error: function (jqXHR, textStatus, errorThrown) {
            $("#result").text(errorThrown);
            turnOffPreLoader($("#defects_info"),"default:50");
        }
    });

服務器端 :

@RequestMapping(value = {"/charts"}, method = RequestMethod.POST)
@ResponseBody
public Chart handleSelectionFiled(@RequestBody Chart chart, HttpServletRequest request, HttpServletResponse response) {
    try{
       chart.doSmth();
    } catch (Exception e) {
       response.setStatus(400);
       response.getWriter().write(e.getMessage());
    }
    return chart;
}

客戶端 :

jQuery.ajax({// just showing how to handle error property
       error: function(jqXHR,error, errorThrown) {  
           if(jqXHR.status && jqXHR.status==400){
                alert(jqXHR.responseText); 
           }else{
               alert("Something went wrong");
           }
      }
}); 

通用Ajax錯誤處理

如果我需要對所有ajax請求進行一些常規的錯誤處理。 我將設置ajaxError處理程序,並在html內容頂部的名為errorcontainer的div上顯示錯誤。

$("div#errorcontainer")
.ajaxError(
    function(e, x, settings, exception) {
        var message;
        var statusErrorMap = {
            '400' : "Server understood the request, but request content was invalid.",
            '401' : "Unauthorized access.",
            '403' : "Forbidden resource can't be accessed.",
            '500' : "Internal server error.",
            '503' : "Service unavailable."
        };
        if (x.status) {
            message =statusErrorMap[x.status];
                            if(!message){
                                  message="Unknown Error \n.";
                              }
        }else if(exception=='parsererror'){
            message="Error.\nParsing JSON Request failed.";
        }else if(exception=='timeout'){
            message="Request Time out.";
        }else if(exception=='abort'){
            message="Request was aborted by the server";
        }else {
            message="Unknown Error \n.";
        }
        $(this).css("display","inline");
        $(this).html(message);
   });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM