Blogger Information
Blog 94
fans 0
comment 0
visits 92684
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
【JS】JSON跨域请求
可乐随笔
Original
411 people have browsed it

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)";
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post