亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么我直接就跳進了error:function里面,沒有進行success:function

$.ajax({
????url:"/ErrorController/ajaxError",
????type:"POST",
????async:false,
????success:function?(data)?{
????????if?(data.status?==200?&&?data.msg?==?"OK"){
????????????alert("succcess");
????????}?else{
????????????alert("error:"?+data.msg);
????????}
????},
????error:?function?(response,ajaxOptions,thrownError)?{
????????alert("error");
????}
});


handler.java

@RestController
public?class?AjaxExceptionHandler?{

????@ExceptionHandler(value?=?Exception.class)
????public?JSONResult?defaultExceptionHandler(HttpServletRequest?request,Exception?e)?throws?Exception{
????????e.printStackTrace();
????????return?JSONResult.errorException(e.getMessage());
????}
}

ajaxError.html:

<!DOCTYPE?html>
<html?lang="en"?xmlns:th="http://www.thymeleaf.org">
<head>
????<meta?charset="UTF-8">
????<title>異常捕獲</title>
????<script?src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>

</head>
<body>
????<h1>測試ajax錯誤異常</h1>

????<script?th:src="@{/js/ajaxerror.js}"></script>
</body>
</html>

controller里面的代碼:

@RequestMapping("/testAjaxError")
public?String?testAjaxError(){
????return?"/thymeleaf/ajaxError";
}


@RequestMapping("/ajaxError")
@ResponseBody
public?JSONResult?ajaxError(){

????int?a=1/0;

????return?JSONResult.ok();
}


正在回答

2 回答

將類注解

@ControllerAdvice

改成

@RestControllerAdvice

就好了


0 回復 有任何疑惑可以回復我~
AjaxExceptionHandler類上沒有加@controlleradvice注解,可能異常統一處理并未生效,導致進入了error?function


0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

為什么我直接就跳進了error:function里面,沒有進行success:function

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號