长轮询聊天室问题

jcl10086 2014-10-15 01:44:30
最近在做一个客服功能 用的长轮询 发送和接受都能实现,有数据 但只要一刷新页面 ,就会出现后台有数据 但前台不调用ajax回调函数 页面无数据 设置超时也会出现这种情况


<script type="text/javascript">

$(document).ready(function(){
receive();
$("button").click(function(){
send();//发送消息
});
});

function send(){
var content=$("#text").val();
var sender=$("#sender").html();
var receiver=$("#receiver").html();
$.ajax({
type:"post",
url:"message?method=sendMessage",
data:{"content":content,"sender":sender,"receiver":receiver},
dataType:"json",
success:function(data){
$("#show").append("我  "+new Date().toLocaleString()+"\r\n  "+content+"\r\n");
$("#text").val("");
}
});
}

function receive(){
//接受消息时,发送和接收,角色互换
var sender=$("#receiver").html();
var receiver=$("#sender").html();
var req=$.ajax({
type:"post",
url:"message?method=getMessage",
data:{"sender":sender,"receiver":receiver},
dataType:"json",
//timeout:60000,
error:function(XMLHttpRequest, textStatus, errorThrown ){
alert("error");
receive();
},
success:function(data,textStatus){
//alert(1);
if("success"==textStatus){
var content=data.content;
var s_name=data.s_name;
var time=data.time;
$("#show").append(s_name+"  "+time+"\r\n  "+content+"\r\n");
receive();
}
}
});
}
...全文
130 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jcl10086 2014-10-15
  • 打赏
  • 举报
回复
jcl10086 2014-10-15
  • 打赏
  • 举报
回复
引用 5 楼 jcl10086 的回复:
[quote=引用 4 楼 arkwrightzhn 的回复:]
success:function(data,textStatus){
                //alert(1);
                if("success"==textStatus){
                    var content=data.content;
                    var s_name=data.s_name;
                    var time=data.time;               
                    $("#show").append(s_name+"  "+time+"\r\n  "+content+"\r\n");   
                    receive();
                }   
            }         
        }
这里面的if("success"==textStatus){一定满足吗?可不可能是success了,但是textStatus不是success,把上面的alert(1)放开,改成alert(textStatus),看看有没有反应。
第一个请求是阻塞后台的,刷新页面在第一个请求未结束又发起一个请求[/quote]有消息alert是success,无数据没触发alert error function也没触发
jcl10086 2014-10-15
  • 打赏
  • 举报
回复
引用 4 楼 arkwrightzhn 的回复:
success:function(data,textStatus){
                //alert(1);
                if("success"==textStatus){
                    var content=data.content;
                    var s_name=data.s_name;
                    var time=data.time;               
                    $("#show").append(s_name+"  "+time+"\r\n  "+content+"\r\n");   
                    receive();
                }   
            }         
        }
这里面的if("success"==textStatus){一定满足吗?可不可能是success了,但是textStatus不是success,把上面的alert(1)放开,改成alert(textStatus),看看有没有反应。
第一个请求是阻塞后台的,刷新页面在第一个请求未结束又发起一个请求
Norris_Zhang 2014-10-15
  • 打赏
  • 举报
回复
success:function(data,textStatus){
                //alert(1);
                if("success"==textStatus){
                    var content=data.content;
                    var s_name=data.s_name;
                    var time=data.time;               
                    $("#show").append(s_name+"  "+time+"\r\n  "+content+"\r\n");   
                    receive();
                }   
            }         
        }
这里面的if("success"==textStatus){一定满足吗?可不可能是success了,但是textStatus不是success,把上面的alert(1)放开,改成alert(textStatus),看看有没有反应。
jcl10086 2014-10-15
  • 打赏
  • 举报
回复
防沉补丁1.0
jcl10086 2014-10-15
  • 打赏
  • 举报
回复
引用 1 楼 arkwrightzhn 的回复:
$.ajax的参数里加一个v:new Date().getTime() 就是一个随机值,否则认为是相同的请求就不发送了。 另外$.ajaxSetup里好像能设置是否清缓存发请求,查一下jquery api.
用get才会产生缓存吧
Norris_Zhang 2014-10-15
  • 打赏
  • 举报
回复
$.ajax的参数里加一个v:new Date().getTime() 就是一个随机值,否则认为是相同的请求就不发送了。 另外$.ajaxSetup里好像能设置是否清缓存发请求,查一下jquery api.

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧