大佬教程收集整理的这篇文章主要介绍了跨域JSONP原理及调用具体示例,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
<script> var url = "http://localhost:8080/crcp/rcp/t99EIDt/testjson.do?jsonp=callBACkfunction"; var script = document.createElement('script'); script.setAttribute('src',url); //load javascript document.getElementsByTagName('head')[0].appendChild(script); //回调函数 function callBACkfunction(data){ var html=JSON.Stringify(data.RESULTSET); alert(html); } </script>
public class TestJson extends ActionSupport{ @Override public String execute() throws Exception { try { JSONObject jsonObject=new JSONObject(); List list=new ArrayList(); for(int i=0;i<4;i++){ Map parAMMap=new HashMap(); parAMMap.put("bank_no",100+i); parAMMap.put("money_type",i); parAMMap.put("bank_name",i); parAMMap.put("bank_type",i); parAMMap.put("bank_status",0); parAMMap.put("en_sign_ways",1); list.add(parAMMap); } JSONArray rows=JSONArray.fromObject(list); jsonObject.put("RESULTSET",rows); httpServletrequest request=ServletActionContext.getrequest(); httpServletResponse response=ServletActionContext.getResponse(); response.setContentType("text/javascript"); Boolean jsonP = false; String cb = request.getParameter("jsonp"); if (cb != null) { jsonP = true; System.out.println("jsonp"); response.setContentType("text/javascript"); } else { System.out.println("json"); response.setContentType("application/x-json"); } response.setCharacterEncoding("UTF-8"); Writer out = response.getWriter(); if (jsonp) { out.write(cb + "("+jsonObject.toString()+")"); System.out.println(jsonObject.toString()); } else{ out.write(jsonObject.toString()); System.out.println(jsonObject.toString()); } } catch (Exception E) { e.printStackTrace(); } return null; } }
$(function(){ jQuery.getJSON("http://localhost:8080/crcp/rcp/t99EIDt/testjson.do?jsonp=?",function(data) { var html=JSON.Stringify(data.RESULTSET); $("#testjsonp").html(html); } ); });
$.ajax({ type:"GET",async :false,url:"http://localhost:8080/crcp/rcp/t99EIDt/testjson.do",dataType:"jsonp",success:function(data){ var html=JSON.Stringify(data.RESULTSET); $("#testjsonp").html(html); },error:function(){ alert("error"); } });
以上是大佬教程为你收集整理的跨域JSONP原理及调用具体示例全部内容,希望文章能够帮你解决跨域JSONP原理及调用具体示例所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。