ajax 跨域处理 jsonp
$.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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |