http-304-状态-status - http请求返回304后ajax的reponseText属性为空字符串

查看:122
本文介绍了http-304-状态-status - http请求返回304后ajax的reponseText属性为空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

我发现http协议在返回304的情况下,xmlhttprequest对象的reponseText属性竟然是空串,如果返回304,应该得到的是本地的缓存数据才对。下面是测试代码:

var _RT = {};
_RT.newRequest = function(){
   var factories = [function() { return new ActiveXObject("Msxml2.XMLHTTP"); },function() { return new XMLHttpRequest(); },function() { return new ActiveXObject("Microsoft.XMLHTTP"); }];
   for(var i = 0; i < factories.length; i++) {
        try {
            var request = factories[i]();
            if (request != null)  return request;
        }
        catch(e) { continue;}
   }
}

_RT.request = function(path){
   var request = new _RT.newRequest()
   request.open("GET", path, false);
   
   try{request.send(null);}
   catch(e){return null;}
   
   if ( request.status == 404 || request.status == 2 ||(request.status == 0 && request.responseText == '') ) return null;
   
   return request.responseText
}
var txt = '';
txt = _RT.request('test.ejs');
console.log(1,txt);
txt = _RT.request('test.ejs');
console.log(2,txt);
txt = _RT.request('test.ejs?v='+Math.random());
console.log(3,txt);

演示地址:http://silian.whyun.com/ajax/rt.html
运行后看控制台,如下现象:

加载次数 请求1 请求2 请求3
第一次加载 200,reponseText非空 304,reponseText为空 200,resonpseText非空
刷新后加载 304,reponseText为空 304,reponseText为空 200,resonpseText非空

最终发现,只要304,reponseText就是空,真是匪夷所思啊。
给出一个刷新后加载的控制台截图:

解决方案

Google Chrome: 48.0.2564.116(正式版本)m(32位)中已不存在该现象。之前发现oogle Chrome: 48.0.2564.97中的确304返回空。

这篇关于http-304-状态-status - http请求返回304后ajax的reponseText属性为空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆