ajax 返回前台数据
来源:互联网 发布:mac桌面壁纸下载网站 编辑:程序博客网 时间:2024/05/20 01:46
之前项目开发中,前台访问后台,后台往前台返回值,都是用@RequestBody 注解往前台返回值,并且返回数据类型不一样,方法的定义返回类型也不一样。后面用到了直接用ajax返回值到前台。觉得很方便,在此做下记录
package com.magingunion.framework.util;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.http.HttpServletResponse;public class AJAXUtil { /** * 返回前台数据(任何类型都可以,String、list等) * * @param response * @param jsonStr */ public static void success(HttpServletResponse response, String jsonStr) { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter out = null; try { out = response.getWriter(); out.write(jsonStr); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } finally{ if(out!=null) out.close(); } } /** * 返回失败状态 * * @param response * @param jsonStr */ public static void failure(HttpServletResponse response, String jsonStr) { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); response.setStatus(500); PrintWriter out = null; try { out = response.getWriter(); out.write(jsonStr); out.flush(); out.close(); } catch (IOException ex) { ex.printStackTrace(); } finally{ if(out!=null) out.close(); } } /** * 返回json格式的msg * * @param bool * @param response Http response */ public static void handleSuccess(HttpServletResponse response, Boolean bool) { PrintWriter out = null; String jsonObj = null; try { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); out = response.getWriter(); if (bool) jsonObj = "{\"msg\":\"SUCCESS\"}"; else jsonObj = "{\"msg\":\"FAIL\"}"; } catch (Exception e) { e.printStackTrace(); jsonObj = "{\"msg\":\"FAIL\"}"; } out.write(jsonObj.toString()); out.flush(); out.close(); } /** * 返回自定义的msg * * @param bool * @param response Http response * @param msg */ public static void handleSuccess(HttpServletResponse response, Boolean bool, String msg) { PrintWriter out = null; String jsonObj = null; try { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); out = response.getWriter(); if (bool) jsonObj = "{\"msg\":\"SUCCESS\","; else jsonObj = "{\"msg\":\"FAIL\","; jsonObj = jsonObj + msg + "}"; } catch (Exception e) { e.printStackTrace(); jsonObj = "{\"msg\":\"FAIL\"}"; } out.write(jsonObj.toString()); out.flush(); out.close(); }}
在方法中,统一用void修饰。并且不需要使用@RequestBody注解,如果返回的是具体数据则用这个返回
AJAXUtil.success(response, JSONObject.toJSONString(data));
如果返回的是成功或者失败,则用
AJAXUtil.handleSuccess(response, true);AJAXUtil.handleSuccess(response, false);
阅读全文
0 0
- ajax 返回前台数据
- 前台Ajax得到后台返回数据
- jquery ajax从不同连接得到数据返回前台
- Json返回查询的数据,前台ajax获取
- 前台jquery+ajax向后台请求数据,后台返回json,前台显示
- 前台使用$.ajax()发送json数据,服务器响应后返回json数据实例(struts2+json+action)
- ajax请求数据,后台提供数据返回json, 前台js拼html,回填
- ajax json 前台传数据到后台,后台接收并返回json 数据
- nodejs 前台页面返回数据
- jquery ajax再次封装,前台调用后台java方法直接返回数据
- ajax 前台后台传输数据
- AJAX数据返回问题
- ajax返回json数据
- Ajax返回XML数据
- Ajax返回JSON数据
- ajax解析返回数据
- ajax传输返回数据
- ajax返回json数据
- 绘画事件
- java生成UUID的方法总结
- jmeter入门——post请求上传附件
- nginx的反向代理及缓存功能。
- .jar !/.... FileNotFoundException解决方法
- ajax 返回前台数据
- 哈夫曼编码/译码器的实现
- POI操作Excel换行
- Airflow学习笔记 --- 运行Python Task
- 内存耗尽产生的crash: malloc: *** error for object 0x: pointer being reallocated was not allocated
- 鼠标事件
- JDK并发包(一)
- mysql 常用
- 拦截器和过滤器的区别