Spring MVC 中Ajax返回字符串
2016-03-08 20:47
381 查看
今天想用Ajax返回一个html的字符串数据。
JavaScript代码:
Java代码:
核心代码:
JavaScript代码:
function saveMarkSolve() { //editor1.sync(); //var s = editor1.html(); $.ajax({ type: "GET", dataType: "html", url: "../exceptions/getHtml", //data: "ExceptionId=" + exceptionId, success: function(msg){ $("#main_container").empty(); $("#main_container").html(msg); },error :function() { alert("请求失败"); } }); }
Java代码:
@RequestMapping(value = "/getHtml", method = RequestMethod.GET) public void getHtml(HttpServletResponse responses){ try{ DefaultHttpClient httpclient = new DefaultHttpClient(); String uri = "http://192.168.0.6:9011/home/WWW"; HttpGet httppost = new HttpGet(uri); //添加http头信息 httppost.addHeader("iv-user", "liu_jun"); //认证token //http post的json数据格式: {"name": "your name","parentId": "id_of_parent"} //JSONObject obj = new JSONObject(); // httppost.setEntity(new StringEntity(obj.toString())); HttpResponse response; response = httpclient.execute(httppost); //检验状态码,如果成功接收数据 int code = response.getStatusLine().getStatusCode(); Map<String, Object> msg = new HashMap<String, Object>(); if (code == 200) { String rev = EntityUtils.toString(response.getEntity());//返回json格式: {"id": "27JpL~j4vsL0LX00E00005","version": "abc"} System.out.println(rev); responses.setContentType("text/html;charset=utf-8"); PrintWriter out=responses.getWriter(); out.print(rev); } else { System.out.println("111"); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
核心代码:
responses.setContentType("text/html;charset=utf-8"); PrintWriter out=responses.getWriter(); out.print(rev);
相关文章推荐
- org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document fro
- Java——一种彻底的面向对象语言
- 隐式(Synchronized)锁和显式(java.util.Lock)锁
- Eclipse下进行SVN提交时报“svn: 过期”错误的解决办法
- Spring学习笔记之IoC容器
- Ibatis:There is no READABLE property named 'roomNo' in class 'java.lang.String'
- 简化MongoAPI(spring-data-mongo)第一部分
- Java内存管理
- [Java]学习笔记(一)
- JDK下载、安装、配置环境变量笔记
- Java 时间日期使用 详细整理 --音
- java内置锁synchronized的可重入性
- Java内存管理:深入Java内存区域
- Java中的&和&&的区别
- Java 环境搭建
- java程序设计基础_陈国君版第五版_第六章例题
- try/catch/finally详解+示例
- Windows 下java环境变量的配置(Windows7 ,8,8.1,10)
- 关于使用Java的事情
- JAVA 字符流与字节流的区别