最近在写一个通知提醒的小东西,显而易见这个肯定要用到ajax这玩意,其实对于ajax异步技术其实还是很好用的。
ajax 一般写法
function check_news(){ var xmlhttp; if (window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest(); } else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 && xmlhttp.status==200){ //这里输入你需要的再页面回填的数据比如alert(xmlhttp.responseText);或者在页面插入innerHTML } } } xmlhttp.open("GET","action",true); xmlhttp.send(); }
继续说通知,要实现实时通知功能就应该规定隔段时间提交一次,用到setinterval(“”,1000);
然后通过ajax提交
setInterval("check_news()",5000); //每隔一秒执行 function check_news(){ var xmlhttp; if (window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest(); } else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 && xmlhttp.status==200){ if(xmlhttp.responseText>0){ document.getElementById("inform").innerHTML="你有"+xmlhttp.responseText+"条新消息"; } } } xmlhttp.open("GET","replyAction_inform",true); xmlhttp.send();
action中
String responseText;
//用response将 responseText传回页面 HttpServletResponse response = ServletActionContext.getResponse();
responseText=String.valueOf(user.getInform()); response.getWriter().write(responseText);
完成。。。。。。。。。。。 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|