首页 新闻 会员 周边

ie8中ajax模拟长连接的问题

0
悬赏园豆:15 [已关闭问题] 关闭于 2018-11-05 20:34

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的结果为:

这是什么原因?

web_小隆的主页 web_小隆 | 初学一级 | 园豆:169
提问于:2018-06-14 17:54
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册