对于ajax请求,后端不能直接返回页面的处理方式

对于前后端分离的情况下,ajax请求在过滤器和拦截器中,都不能直接返回页面,那么应该怎么处理呢?

response.setContentType("application/json; charset=utf-8");
PrintWriter writer = null;
try {
writer = response.getWriter();
ObjectMapper mapper = new ObjectMapper();
ajaxJson ajaxJson = new AjaxJson();
ajaxJson .setSuccess(false);
ajaxJson .setMessage(code);
String errorMsg = mapper.writeValueAsString(ajaxJson);
writer.write(errorMsg);
writer.flush();
} catch (Exception ex) {
LOGGER.error("输出错误响应信息异常:" + ex.getMessage());
throw new RuntimeException("输出错误响应信息异常:" + ex.getMessage(), ex);
} finally {
if (writer != null) {
writer.close();
}
}
}

你可能感兴趣的:(高并发)