当前位置:Gxlcms > JavaScript > 简单了解JQuery+ajax+jsonp跨域访问

简单了解JQuery+ajax+jsonp跨域访问

时间:2021-07-01 10:21:17 帮助过:3人阅读

Jsonp(JSON with Padding)是资料格式 json 的一种“使用模式”,可以让网页从别的网域获取资料。
一. 客户端

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>Insert title here</title>
  6. <script type="text/javascript" src="resource/js/jquery-1.7.2.js"></script>
  7. </head>
  8. <script type="text/javascript">
  9. $(function(){
  10. /*
  11. //简写形式,效果相同
  12. $.getJSON("http://app.example.com/base/json.do?sid=1494&busiId=101&jsonpCallback=?",
  13. function(data){
  14. $("#showcontent").text("Result:"+data.result)
  15. });
  16. */
  17. $.ajax({
  18. type : "get",
  19. async:false,
  20. url : "http://app.example.com/base/json.do?sid=1494&busiId=101",
  21. dataType : "jsonp",//数据类型为jsonp
  22. jsonp: "jsonpCallback",//服务端用于接收callback调用的function名的参数
  23. success : function(data){
  24. $("#showcontent").text("Result:"+data.result)
  25. },
  26. error:function(){
  27. alert('fail');
  28. }
  29. });
  30. });
  31. </script>
  32. <body>
  33. <p id="showcontent">Result:</p>
  34. </body>
  35. </html>

二. 服务器端

以上就是简单了解JQuery+ajax+jsonp 跨域访问 的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行