1.。页面请求 ajax.jsp:
$.get("AjaxServer?name=" + $("#userName").val(),null,function(data){
$("#result").html(data);
}) ;
ajaxserver.java
Integer inte = (Integer) httpServletRequest.getSession().getAttribute("total");
int temp = 0;
if (inte == null) {
temp = 1;
} else {
temp = inte.intValue() + 1;
}
httpServletRequest.getSession().setAttribute("total",temp);
if(old == null || old.length() == 0){
out.println("用户名不能为空");
} else{
// String name = URLDecoder.decode(old,"UTF-8");
// byte[] by = old.getBytes("ISO8859-1");
// String name = new String(by,"utf-8");
// String name = URLDecoder.decode(old,"utf-8");
//3.校验操作
// String name = old;
if(name.equals("wangxingkui")){
//4。和传统应用不同之处。这一步需要将用户感兴趣的数据返回给页面段,而不是将一个新的页面发送给用户
//写法没有变化,本质发生了改变
out.println("用户名[" + name + "]已经存在,请使用其他用户名, " + temp);
} else{
out.println("name:" + name + "keyi zhuce," + temp);
System.out.println("cunzai");
}
}
} catch(Exception e){
e.printStackTrace();
}
用IE请求,http://localhost:8080/ajax.jsp,相同的userName,返回的都是1,读的都是IE的缓存。
而firefox不是。
2.。当修改URL编码,为URL编码加上时间戳。
url = convertURL(url);
$.get(url,null,function(data){
$("#result").html(data);
//给url地址增加时间戳,骗过浏览器,不读取缓存
function convertURL(url) {
//获取时间戳
var timstamp = (new Date()).valueOf();
//将时间戳信息拼接到url上
//url = "AJAXServer"
if (url.indexOf("?") >= 0) {
url = url + "&t=" + timstamp;
} else {
url = url + "?t=" + timstamp;
}
return url;
}