Jquery发送ajax请求以及datatype参数为text/JSON方式
$.ajax({
type: "POST",
url: "<%=basePath%>getAllUser.action?randomNum="+new Date().getTime(),
data : {},
datatype : 'text',
cache: false,
async: false,
success:function(data) {
strHtml = data的处理结果; //对data数据进行处理,拼接成html代码块
$("#userList").html(strHtml); //也可以使用:$("#userList").append(strHtml);
}
},
error:function(){
alert("获取用户信息失败,请联系管理员!");
}
});//end ajax
1.2 后台java代码处理:
public String getAllUser(){
response.setContentType("text;charset=UTF-8");// 设置返回的文档类型
response.setHeader("Cache-Control", "no-cache");
response.setCharacterEncoding("UTF-8");
PrintWriter out = null;
String text = "";
try {
out = response.getWriter();
} catch (Exception e) {
text = "false";
logger.error(e.getMessage());
}
//TODO 获取所有用户信息,遍历
//text = "userId1,userId2,userId3";
out.print(text);
out.flush();
out.close();
return null;
}
2、方式二:datatype:'JSON'
$.ajax({
type: "POST",
url: "<%=basePath%>getAllUser.action",
datatype : 'JSON',
cache:false,
success : function(data) {
$.each(data,function(entryIndex,entry){
var userId=entry.id;
var userName=entry.userName;
......
// TODO strHtml = 构造显示的html代码块;
// 以追加方式进行填充内容
$("#userList").append(strHtml);
});//end each
}// end success
});//end ajax
2.2 后台java代码处理:
public String getAllUser() throws Exception {
try {
response = ServletActionContext.getResponse();
response.setContentType("application/json");
response.setCharacterEncoding("gbk");
List<User> userList = new ArrayList<User>();
for (int i = ; i < size; i++) {
User user = new User();
user.setUserId(i);
user.setUserName("kobicc" + i);
......
//这个逻辑需要从数据库中获取结果即可。
userList.add(user);
}
//com.google.gson.Gson gson-1.5.jar包中的类文件
Gson gson = new Gson();
String jsonString = gson.toJson(userList);
PrintWriter out = response.getWriter();
out.print(jsonString);
out.flush();
out.close();
return null;
} catch (Exception e) {
throw new RuntimeException("获取用户信息失败!");
}
}
3、使用gson-1.5.jar包和json-2.2.jar包处理JSON代码
$.ajax({
type: "POST",
url: "<%=basePath%>getAllUser.action",
datatype : 'JSON',
cache:false,
success : function(data) {
//!!!重要,需要使用一下这个将data变为JSON对象
var json = eval_r("("+data+")");
//alert(json.nUserListCount);
//TODO 进行后续逻辑操作
}// end success
});//end ajax
3.2.2 后台java代码处理:
public void getAllUser() throws Exception {
try {
response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
//net.sf.json.JSONArray json-2.2.jar包中的类文件
JSONArray objs = new JSONArray();
JSONObject json = new JSONObject();
JSONObject returnJSON = new JSONObject();
List<User> userList = userService.getAllUser();
int nUserListCount = ;
nUserListCount = userList.size();
for (User user : userList) {
json.element("userNo", user.getUserNo());
json.element("userName", user.getUserName());
......
objs.add(json);
}
returnJSON.put("userList", objs);
returnJSON.put("nUserListCount", nUserListCount);
response.getWriter().write(returnJSON.toString());
} catch (Exception e) {
logger.error("获取用户信息失败!", e);
throw new RuntimeException("获取用户信息失败!");
}
}
Jquery发送ajax请求以及datatype参数为text/JSON方式的更多相关文章
- JQuery发送ajax请求不能用数组作为参数
JQuery发送ajax请求不能用数组作为参数,否则会接收不到参数, 一.js代码如下: $('#delete-button').click(function(){ var select ...
- 学习AJAX必知必会(4)~JQuery发送Ajax请求
一.JQuery发送Ajax请求 ■ 对于get和post请求,jQuery内部封装了Ajax请求的4个步骤和数据格式的设置 ■ 对于Ajax通用请求,jQuery内部封装了Ajax请求的4个步骤和数 ...
- jQuery发送ajax请求
利用jquery发送ajax请求的几个模板代码. $.ajax({ async : false, type: 'POST', dataType : "json", url: &qu ...
- jQuery发送Ajax请求以及出现的问题
普通jQuery的Ajax请求代码如下: $.ajax({ type: 'POST', url: "http://xxx/yyy/zzz/sendVerifyCode", data ...
- IE9下JQuery发送ajax请求失效
最近在做项目的时候,测试PC端网页,在IE9下会失效,不能正常的发送POST请求,经过仔细的排查,发现是IE9下JQuery发送ajax存在跨域问题. 目前有两种解决方案: 解决方案一: 设置浏览 ...
- 关于解决JQuery发送Ajax请求后,IE缓存数据不更新的问题
http://www.cnblogs.com/lys_013/archive/2013/08/07/3243435.html 今天在做ajax页面无刷新请求后台服务器数据的时候,IE下遭遇Ajax缓存 ...
- python测试开发django-50.jquery发送ajax请求(get)
前言 有时候,我们希望点击页面上的某个按钮后,不刷新整个页面,给后台发送一个请求过去,请求到数据后填充到html上,这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新.Ajax可以完美的 ...
- jquery发送ajax请求返回数据格式
jquery向服务器发送一个ajax请求后,可以返回多种类型的数据格式,包括:html,xml,json,text等. 1.html格式的数据 "<div class='comment ...
- JQuery发送Ajax请求出现 500 Internal Server Error
ajax返回,readyState=4,status=500,chrome f12提示,提示服务器内部错误 我采用 http://q.cnblogs.com/q/69745/的解决办法根本不行,也不是 ...
随机推荐
- 一步一步重写 CodeIgniter 框架 (10) —— 使用 CodeIgniter 类库(续)
上一节简单实现了 CI 的类库扩展模型,所以 _ci_load_class 和 _ci_init_class 写的不是很完备.根据上节课的分析,当 system/libraries 目录下存在 Ema ...
- 多模块Maven项目怎样使用javadoc插件生成文档
需求 近期要对一个项目结构例如以下的Maven项目生成JavaDoc文档. Project |-- pom.xml ...
- mysql 参数optimizer_switch
mysql 5.1中开始引入optimizer_switch, 控制mysql优化器行为.他有一些结果集,通过on和off控制开启和关闭优化器行为.使用有效期全局和会话两个级别,在5.5中optimi ...
- ListView属性解释
1.android:scrollbarStyle 定义滚动条的样式和位置 参考:http://www.trinea.cn/android/android-scrollbarstyle/ 2.andro ...
- C#反射 入门学习 01
前言 获取方法的相关信息的两种形式 反射是一种允许用户获得类信息的C#功能,Type对象映射它代表的底层对象: 在.Net 中, 一旦获得了Type对象,就可以使用GetMethods()方法 ...
- Qt核心剖析: 寻找 QObject 的源代码
本来打算把<Qt学习之路>作为一个类似教程的东西,所以就不打算把一些关系到源代码的内容放在那个系列之中啦.因此今天就先来看一个新的开始吧!这个系列估计不会进展很快,因为最近公司里面要做 f ...
- spoj 1812 lcsII (后缀自动机)
spoj 1812 lcsII (后缀自动机) 题意:求多个串的lcs,最多10个串,每个串最长10w 解题思路:后缀自动机.先建好第一个串的sam,然后后面的串拿上去跑(这个过程同前一题).sam上 ...
- Linux less命令
less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...
- java中的object类
在Java中,任何一个类都扩展来自Object类.当没有为某一个类定义父类时,Java会自动定义Object类为其父类. object类的一些常用方法: (1)public String toStri ...
- yii phpexcel自己主动生成文件保存到server上
近期再整一个报表任务,每天必须把表导出来按excel格式发送邮件给管理员,利用phpexcel把表保存到server上.然后再通过phpmailer发送就ok. ob_end_clean(); ...