前段页面
$.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("
});
},
error: function(){
alert('fail');
}
});
});
服务端
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; }
总结
以上是编程之家为你收集整理的ajax 跨域处理 jsonp全部内容,希望文章能够帮你解决ajax 跨域处理 jsonp所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。