為什么我直接就跳進了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();
}
2020-06-10
將類注解
改成
就好了
2019-12-26