首页 技术 正文
技术 2022年11月6日
0 收藏 366 点赞 975 浏览 1796 个字

JSONP 教程

Jsonp(JSON with Padding) 是 json 的一种”使用模式”,可以让网页从别的域名(网站)那获取资料,即跨域读取数据。

为什么我们从不同的域(网站)访问数据需要一个特殊的技术(JSONP )呢?这是因为同源策略。

同源策略,它是由Netscape提出的一个著名的安全策略,现在所有支持JavaScript 的浏览器都会使用这个策略。

JSONP 应用

1. 服务端JSONP格式数据

假如客户想访问 : http://www.baidu.com/try/ajax/jsonp.php?jsonp=callbackFunction。

假设客户期望返回JSON数据:[“customername1″,”customername2”]。

真正返回到客户端的数据显示为: callbackFunction([“customername1″,”customername2”])。

服务端文件jsonp.php代码为:

  1. <?php
  2. header('Content-type: application/json');
  3. //获取回调函数名
  4. $jsoncallback = htmlspecialchars($_REQUEST ['jsoncallback']);
  5. //json数据
  6. $json_data ='["customername1","customername2"]';
  7. //输出jsonp格式的数据
  8. echo $jsoncallback ."(". $json_data .")";
  9. ?>

 

2. 客户端实现 callbackFunction 函数

  1. <script type="text/javascript">
  2. function onCustomerLoaded(result, methodName)
  3. {
  4. var html ='<ul>';
  5. for(var i =0; i < result.length; i++)
  6. {
  7. html +='<li>'+ result[i]+'</li>';
  8. }
  9. html +='</ul>';
  10. document.getElementById('divCustomers').innerHTML = html;
  11. }
  12. </script>

 

页面展示

<div id="divCustomers"></div>

  

客户端页面完整代码

  1. <!DOCTYPE html >
  2. <head>
  3. <title>JSONP 实例</title>
  4. </head>
  5. <body>
  6. <div id="divCustomers"></div>
  7. <script type="text/javascript">
  8. function callbackFunction(result, methodName)
  9. {
  10. var html ='<ul>';
  11. for(var i =0; i < result.length; i++)
  12. {
  13. html +='<li>'+ result[i]+'</li>';
  14. }
  15. html +='</ul>';
  16. document.getElementById('divCustomers').innerHTML = html;
  17. }
  18. </script>
  19. <script type="text/javascript" src="http://www.baidu.com/try/ajax/jsonp.php?jsoncallback=callbackFunction"></script>
  20. </body>
  21. </html>

 


jQuery 使用 JSONP

以上代码可以使用 jQuery 代码实例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>JSONP 实例</title>
  5. <script src="http://apps.bdimg.com/libs/jquery/1.8.3/jquery.js"></script>
  6. </head>
  7. <body>
  8. <div id="divCustomers"></div>
  9. <script>
  10. $.getJSON("http://www.baidu.com/try/ajax/jsonp.php?jsoncallback=?", function(data){
  11. var html ='<ul>';
  12. for(var i =0; i < data.length; i++)
  13. {
  14. html +='<li>'+ data[i]+'</li>';
  15. }
  16. html +='</ul>';
  17. $('#divCustomers').html(html);
  18. });
  19. </script>
  20. </body>
  21. </html>

 

下一篇: New Barns
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,024
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,514
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,362
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,776
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,854