js代码:
var ws = {}; var timer = null; //定时器 var count = 0; //连接错误次数 //发送请求 ws.send = function(data){ if(data){ var data = "data=" + data; }else{ var data = "data="; } $.ajax({ url: "http://localhost/test/data.php", type: "get", data: data, async: false, cache: false, timeout: 5000, //请求超时或其它错误 error: function(xhr,txt){ // 状态码 console.log(xhr.status); // 状态 console.log(xhr.readyState); console.log(txt); count++; //连续错误超过三次关闭连接 if(count>2){ //some code here ws.close(); } }, success: function(data){ count = 0; console.log(data); //some code here } }); } //打开连接 ws.open = function(){ //发送空字节告诉对方自己存在 timer = setInterval(function(){ ws.send(); },300); } //关闭连接 ws.close = function(){ console.log("connection is close"); clearInterval(timer); } ws.open();
后台php代码:
<?php if(isset($_GET["data"])) $data = $_GET["data"]; else{ echo "your data is null"; return; } if(empty($data)){ echo "keep connection"; } else echo "your data is ".$data; ?>
上面是主要的代码,在chrome中可以正确显示结果,而ie8的结果为:
这是什么原因?