ajax 跨域处理 jsonp

前端之家收集整理的这篇文章主要介绍了ajax 跨域处理 jsonp前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

  1. 前段页面 <script type="text/javascript">
jQuery(document).ready(function(){
$.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

猜你在找的Ajax相关文章