今天想用Ajax返回一个html的字符串数据。

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);

Spring MVC 中Ajax返回字符串的更多相关文章

  1. Spring MVC 中请求返回之后的页面没法加载css、js等静态文件

    1.是否被拦截,这个在Web.xml配置中servlet拦截是“/”,如果是则 a.使用spring MVC 的静态资源文件 <!-- 静态文件访问,主要是针对DispatcherServlet ...

  2. Spring MVC 中 AJAX请求并返回JSON

    一.以ModelAndView的方式返回 先看下JavaScript代码: /** * 保存-同步(版本控制库) */ function saveSynchronizedVcHorse(obj) { ...

  3. Spring MVC中Ajax实现二级联动

    今天写项目遇到了二级联动,期间遇到点问题,写个博客记录一下. 后台Controller: @RequestMapping("/faultType") @ResponseBody p ...

  4. Spring MVC中Controller返回值void时报错

    Controller如下: 当使用url访问该处理器方法时,报错如下: 26-Jan-2019 21:16:28.105 警告 [http-nio-8080-exec-39] org.springfr ...

  5. Spring MVC 中的基于注解的 Controller【转】

    原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...

  6. Spring MVC中基于注解的 Controller

         终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...

  7. Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法

    Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法 在Action中方法的返回值都是字符串行,一般情况是返回某个JSP,如: return "xx" ...

  8. Http请求中Content-Type讲解以及在Spring MVC中的应用【转】

    完全引用自: http://blog.csdn.net/blueheart20/article/details/45174399#t1   此文讲得很清晰,赞! 引言: 在Http请求中,我们每天都在 ...

  9. Http请求中Content-Type和Accept讲解以及在Spring MVC中的应用

    在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值,以及在 ...

随机推荐

  1. 【tarjan】BZOJ2140-稳定婚姻

    又名NTR的故事 [题目大意] n对夫妻Bi和Gi.若某男Bi与某女Gj曾经交往过,他们有私奔的可能性.不妨设Bi和Gj旧情复燃,进而Bj会联系上了他的初恋情人Gk,以此递推.若在Bi和Gi离婚的前提 ...

  2. [CodeForces-763C]Timofey and remoduling

    题目大意: 告诉你一个长度为n的等差数列在模m意义下的乱序值(互不相等),问是否真的存在满足条件的等差数列,并尝试构造任意一个这样的数列. 思路: 首先我们可以有一个结论: 两个等差数列相等,当且仅当 ...

  3. Ubuntu 12.04下Hadoop 2.2.0 集群搭建(原创)

    现在大家可以跟我一起来实现Ubuntu 12.04下Hadoop 2.2.0 集群搭建,在这里我使用了两台服务器,一台作为master即namenode主机,另一台作为slave即datanode主机 ...

  4. Spring Boot中Request method 'PUT' not supported

    在项目中使用restful风格put提交时报错,是由于form表单中的th:href引起的(支持post提交),改为th:action即可

  5. java开发_数字转换汉语中人民币的大写_完整版

    做这个应用,源于突然的一个想法:看到发票上面的数字要转换成汉语中人民币的大写 于是就有了下面的这些事儿..... 先看看运行效果: ================================== ...

  6. Codeforces Beta Round #92 (Div. 1 Only) A. Prime Permutation 暴力

    A. Prime Permutation 题目连接: http://www.codeforces.com/contest/123/problem/A Description You are given ...

  7. Linux下使用ping出现destination is unreachable的问题可能性

    ping时出现request time out和destination is unreachable request time out是指icmp包发出后,长时间没有回应,所以会产生request t ...

  8. nginx出现504 Gateway Time-out的解决思路

    http://www.xbc.me/nginx-fix-504-gateway-timeout/ 在安装完Nginx+PHP-fpm+Mysql后 (如何安装LNMP环境,请参考快速配置LNMP环境N ...

  9. 直接拿来用!最火的iOS开源项目(一~三)

    结束了GitHub平台上“最受欢迎的Android开源项目”系列盘点之后,我们正式迎来了“GitHub上最受欢迎的iOS开源项目”系列盘点.今天,我们将介绍20个在GitHub上非常受开发者欢迎的iO ...

  10. Add current boost to a USB charger

    The popular USB interface can charge a portable device while transferring data. But for high-capacit ...