ajax为何不能定时刷新啊

街头诗人1 2017-02-26 03:18:03
$(function(){
getData();
window.setInterval(getData,3000);

});
function getData(){
var url= "<%=basePath %>msg";
var data = {content:${msg.msgContent}};
$.ajax({
dataType:"json";
type="post",
async:true,
url:url,
data:data,
timeout:2000,
success:function(data){
("#showMsg").html(data);
("#showMsg").slideDown(300);
("#showMsg").slideUp(300);
},
error:function(){
alert("请稍后再试");
},

});
}
...全文
217 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
蚂蚁上树 2017-03-02
  • 打赏
  • 举报
回复
type="post", 改为type:"post" 另外f12 看看控制台报的什么错。跟踪headers 和preview和response
二月十六 2017-02-27
  • 打赏
  • 举报
回复
这俩写的有问题呢 dataType:"json";改成dataType:"json" type="post",改成type:"post",
jac-song 2017-02-27
  • 打赏
  • 举报
回复
定时没有问题,在chrome浏览器下按F12打开开发者工具看看你的GetData方法是否正确!!!
Go 旅城通票 2017-02-27
  • 打赏
  • 举报
回复
引号括起你的数据,要不非布尔或者数字内容会报错 var data =' {content:${msg.msgContent}}';
街头诗人1 2017-02-26
  • 打赏
  • 举报
回复
servlet代码 response.setContentType("text/xml;charset=utf-8"); //最大的id int maxId=0; MsgDao md=new MsgDao(); //判断是否第一次查询 if(first){ //查出当前最大的id maxId=md.maxId(); if(maxId!=0){ //存入seession request.getSession().setAttribute("nowMax", maxId); request.getRequestDispatcher("index.jsp").forward(request, response); } first=false; }else{ //查出当前最大的id maxId=md.maxId(); //取出seesion中存储的最大的id int nowId=(Integer) request.getSession().getAttribute("nowMax"); //判断是否有新消息 if(maxId>nowId){ request.getSession().setAttribute("nowMax", nowId); //调用查询消息方法 MsgInfo msg=md.queryUserById(nowId); //存储数据 request.setAttribute("msg", msg); //转发 request.getRequestDispatcher("index.jsp").forward(request, response); }else{ request.getRequestDispatcher("index.jsp").forward(request, response); } }

52,797

社区成员

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

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