加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax 跨域处理 jsonp

发布时间:2020-12-15 21:02:53 所属栏目:百科 来源:网络整理
导读:前段页面 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", suc

  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; }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读