在getAjacError方法里面除0操作,跑到ajacError.js里面,直接就进了error,无法请求成功??
publicclassHelloWorld{{"name":"\u767d\u5bcc\u7f8e","say":"\u54b1\u4eec\u7ed3\u5a5a\u5427!"}
将 @ControllerAdvice 换成 @RestControllerAdvice
进error是因为js无法识别你的data对象,因为你的data对象不是json格式,所以进入了你定义的回调函数的error方法。其实请求是成功的,只是你用的注解不是@RestControllerAdvice
@ExceptionHandler(value = Exception.class) public void exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception e, Model model) throws Exception { if (isAjax(request)) { // 向response中写json数据 response.setCharacterEncoding("utf-8"); response.setContentType("application/json; charset=utf-8"); PrintWriter writer = response.getWriter(); writer.write(gson.toJson(JsonResult.errorException(e.getMessage()))); } else { request.setAttribute("exception", e); request.setAttribute("url", request.getRequestURL()); request.getRequestDispatcher("/templates/error.html").forward(request, response); } }
同问啊
怎么解决的,我也感觉很奇怪
已根据@玩蜡笔小破孩同学的方法解决了问题,感谢