登录  /  注册
博主信息
博文 94
粉丝 0
评论 0
访问量 89416
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
【JS】JSON跨域请求
可乐随笔
原创
379人浏览过

JSON跨域请求

  • 同源“案例”策略
  • 仅限“协议、域名、端口”三者都相同,才可以相互访问
  • 否则跨域错误 : CORS
  • 原则:利用 script.src 可以跨域发起请求

1.跨域请求:PHP函数回调(带参)

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>跨域请求:PHP函数回调(带参)</title>
  8. </head>
  9. <body>
  10. <script>
  11. //data:跨域数据
  12. //1.函数声明:当前脚本
  13. function hello(data){
  14. // console.log(data.name)
  15. const str =`
  16. <ul>
  17. <li>序号:${data.id}</li>
  18. <li>姓名:${data.name}</li>
  19. <li>邮箱:${data.email}</li>
  20. </ul>
  21. `
  22. document.body.insertAdjacentHTML('afterbegin',str)
  23. }
  24. </script>
  25. <!-- 2. 调用函数语句,在跨域脚本中-->
  26. <!-- <script src="http://jsonp.io/demo.js"></script> -->
  27. <script>
  28. function getJSON(url){
  29. const script = document.createElement('script');
  30. script.src = url;
  31. document.body.append(script);
  32. }
  33. getJSON('http://jsonp.io/demo1.php?callback=hello&id=1');
  34. </script>
  35. </body>
  36. </html>

PHP示范:

  1. <?php
  2. $users = [
  3. ['id' => 1, 'name' => '老六', 'email' => 'nx99@qq.com'],
  4. ['id' => 2, 'name' => '老七', 'email' => 'nx88@qq.com'],
  5. ['id' => 3, 'name' => '老八', 'email' => 'nx77@qq.com'],
  6. ];
  7. $id = $_GET['id'];
  8. $callback = $_GET['callback'];
  9. $user = array_filter($users, function ($item) use ($id) {
  10. return $item['id'] == $id;
  11. });
  12. $data = json_encode(array_pop($user));
  13. //通知返回字符串的形式,将回调函数写回去,在原页面调用数据,规避了跨域调用数据
  14. echo "$callback($data)";

2.跨域请求:PHP跨域回调(带参)

HTML代码示范:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>跨域请求:PHP跨域回调(带参)</title>
  8. </head>
  9. <body>
  10. <script>
  11. //data:跨域数据
  12. //1.函数声明:当前脚本
  13. function say(data) {
  14. // console.log(data.name)
  15. const str = `
  16. <ul>
  17. <li>序号:${data.id}</li>
  18. <li>姓名:${data.name}</li>
  19. <li>邮箱:${data.email}</li>
  20. </ul>
  21. `;
  22. document.body.insertAdjacentHTML('afterbegin', str);
  23. }
  24. </script>
  25. <!-- 2. 调用函数语句,在跨域脚本中-->
  26. <!-- <script src="http://jsonp.io/demo.js"></script> -->
  27. <script>
  28. function getJSON(url) {
  29. //ajax
  30. fetch(url)
  31. .then(res => res.json())
  32. .then(json => say(json));
  33. }
  34. getJSON('http://jsonp.io/demo2.php?callback=say&id=1');
  35. </script>
  36. </body>
  37. </html>

PHP代码示范

  1. <?php
  2. header("Access-Control-Allow-Origin: *");
  3. $users = [
  4. ['id' => 1, 'name' => '老六', 'email' => 'nx99@qq.com'],
  5. ['id' => 2, 'name' => '老七', 'email' => 'nx88@qq.com'],
  6. ['id' => 3, 'name' => '老八', 'email' => 'nx77@qq.com'],
  7. ];
  8. $id = $_GET['id'];
  9. $callback = $_GET['callback'];
  10. $user = array_filter($users, function ($item) use ($id) {
  11. return $item['id'] == $id;
  12. });
  13. // $data = json_encode(array_pop($user));
  14. echo json_encode(array_pop($user));
  15. // echo "callback($data)";
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学