前段页面 <script type="text/javascript">
$.ajax({
type: "get",
async: false,
url: 'http://shanghai.51fmzg.com/findNoticeMessage.htm',
dataType: "jsonp",
jsonp: "callback",
jsonpCallback:"flightHandler",
success: function(json){
$.each(json,function(idx,item){
$("div.notice ul").after("<li><div class='newtitle'>"+item.content+"</div><div class='time'>"+item.sendDate+"</div><div class='clear'></div></li>")
});
},
error: function(){
alert('fail');
}
});
});
</script>
服务端
public ModelAndView findNoticeMessage(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { List noticeMessageList = messageManager.findNoticeMessage(0,3); Collections.reverse(noticeMessageList); List jsonList=new ArrayList(); String message="["; for (int i = 0; i < noticeMessageList.size(); i++) { Map map=(Map)noticeMessageList.get(i); if(i>0){ message=message+","; } String string =map.get("sendDate").toString(); message+="{'content':'"+map.get("content").toString().trim()+"','sendDate':'"+map.get("sendDate").toString().substring(0,16)+"'}"; } message+="]"; JSONArray ja = JSONArray.fromObject(noticeMessageList); response.setContentType("text/plain"); PrintWriter out = null; try { out = new PrintWriter(new OutputStreamWriter(response .getOutputStream(),"utf-8")); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } String callback = request.getParameter("callback"); out.println(callback+"("+message+")"); out.flush(); out.close(); return null; }
原文链接:https://www.f2er.com/ajax/166732.html