摘自: http://hi.baidu.com/erik168/item/41a5910c1eca0627a0312dd8
【未亲自测试,请斟酌参考】
javascript代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<script> function getXHR() { return window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); } var log = []; var xhr = getXHR(); //xhr.open('get', '302local.php'); xhr.open('get', '302net.php'); xhr.onreadystatechange = function () { try { log.push('status:' + xhr.status + ';readyState:' + xhr.readyState); } catch (ex) {} if (xhr.readyState == 4) { alert(log.join('\n')); } }; xhr.send(null); </script> |
302local.php:
header(“Location:hello.php”);
302net.php:
header(“Location:http://www.baidu.com”);
测试浏览器:
IE7
Firefox3.5
Opera9.6
Safari4
结论:
1.在重定向到本域时,无论如何都无法获得302状态码,XMLHttpRequest对象会直接读取重定向资源的内容
Fx3.5 alert:
status:200;readyState:2
status:200;readyState:3
status:200;readyState:4
IE7 alert:
status:200;readyState:4
Safari4 alert:
status:200;readyState:2
status:200;readyState:3
status:200;readyState:4
Opera9.6 alert:
status:200;readyState:3
status:200;readyState:4
2.在重定向到其他域时:
Safari和Opera会哑掉
IE会问你“可能导致安全风险,是否继续”,是的话取得status是200,否的话取得status是0
Firefox会获得302的status
Fx3.5 alert:
status:302;readyState:2
status:302;readyState:4
IE7 alert:
status:200;readyState:4
or
status:0;readyState:4