疫情期间手机直线:18622734798
当前位置:首页网站设计:基础篇 → 全部信息
Ajax缓存和编码问题的最终解决方案
更新时间:2012/7/26 点击:1703次
ajax缓存和编码问题不难解决,下面是解决方法。
编码问题
默认使用UTF-8,如果一旦发现对象找不到的情况,可能js中输入了中文,同时js的编码格式可能为gb2312,可用记事本打开js,另存为utf-8格式的文档。
通过XMLHttpRequest获取的数据,默认的字符编码是UTF-8,如果前端页面是GB2312或者其它编码,显示获取的数据就是乱码。通过XMLHTTPRequest,POST的数据也是UTF-8编码,如果后台是GB2312或者其他编码也会出现乱码。
Cache缓存问题
由于IE的缓存处理机制问题,每次通过XMLHttpRequest访问动态页面返回的总是首次访问的内容,解决方法有:
1. 客户端通过添加随机字符串解决。如:
var url = 'http://www.bothv.com/';
url += '?temp=' + new Date().getTime();
url += '?temp=' + Math.random();
2. 在HTTP headers禁止缓存。如:
HTTP:
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="Cache-Control" content="no-cache, must-revalidate" />
<meta http-equiv="expires" content="Thu, 01 Jan 1970 00:00:01 GMT" />
<meta http-equiv="expires" content="0" />
PHP:
header("Expires: Thu, 01 Jan 1970 00:00:01 GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
ASP:
Response.expires=0
Response.addHeader("pragma","no-cache")
Response.addHeader("Cache-Control","no-cache, must-revalidate")
JSP:
response.addHeader("Cache-Control", "no-cache");
response.addHeader("Expires", "Thu, 01 Jan 1970 00:00:01 GMT");
3. 在XMLHttpRequest发送请求之前加上:
XMLHttpRequest.setRequestHeader("If-Modified-Since","0");
XMLHttpRequest.send(null);
  • 疫情期间手机直线:18622734798    服务邮箱:service@nfree.cn     QQ:1448132697
  • 地址:天津市河西区围堤道146号华盛广场B座22楼    

    津公网安备 12010302001042号

  • CopyRight 2006~2024 All Rights Reserved 天津市华易动力信息科技有限公司