Blogger Information
Blog 52
fans 0
comment 3
visits 42423
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
html学习:第16章 细说跨域请求(CORS/JSONP)
王小飞
Original
767 people have browsed it

跨域请求

方法一:CORS 跨域资源共享

同源策略

  • 多个页面的协议, 域名, 端口完全相同, 则认为他们遵循了”同源策略”

  • 同源策略是一种安全机制

  • 浏览器禁止通过脚本发起跨域请求, 如XMLHttpRequest,但是允许通知 html 标签属性跨域
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <button>跨域请求-CORS</button>
  10. <h2></h2>
  11. <script>
  12. var btn = document.querySelector("button");
  13. btn.addEventListener(
  14. "click",
  15. function () {
  16. // 1. 创建Ajax对象
  17. var xhr = new XMLHttpRequest();
  18. // 2. 监听请求
  19. xhr.onreadystatechange = function () {
  20. if (xhr.readyState === 4 && xhr.status === 200) {
  21. console.log(xhr.responseText);
  22. document.querySelector("h2").innerHTML = xhr.responseText;
  23. }
  24. };
  25. // 3. 初始化请求参数
  26. xhr.open("GET", "http://seo.cc/test1.php", true);
  27. // 4. 发送请求
  28. xhr.send(null);
  29. },
  30. false
  31. );
  32. </script>
  33. </body>
  34. </html>

php代码

  1. <?php
  2. header('Access-Control-Allow-Origin:http://p.cc');
  3. header('Access-Control-Allow-Origin:http://seo.cc');
  4. echo "这是通过CORS进行的跨域访问!";
  5. flush();

方法二:jsonp 跨域请求

2. JSONP

  • 同源策略禁止通过脚本发起跨域请求, 但是允许通过标签和属性发起一个跨域
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <button>跨域请求-JSONP</button>
  10. <script>
  11. // 1. 准备好回调处理函数
  12. function handle(jsonData) {
  13. console.log(jsonData);
  14. var data = JSON.parse(jsonData);
  15. console.log(data);
  16. // 将接口返回的数据渲染到页面中
  17. var ul = document.createElement("ul");
  18. ul.innerHTML += "<li>" + data.title + "</li>";
  19. ul.innerHTML += "<li>姓名:" + data.user.name + "</li>";
  20. ul.innerHTML += "<li>邮箱: " + data.user.email + "</li>";
  21. document.body.appendChild(ul);
  22. }
  23. // 2. 点击按钮发起一个基于JSONP的跨域请求
  24. var btn = document.querySelector("button");
  25. btn.addEventListener(
  26. "click",
  27. function () {
  28. var script = document.createElement("script");
  29. script.src = "http://w.cc/test2.php?jsonp=handle&id=2";
  30. document.head.appendChild(script);
  31. },
  32. false
  33. );
  34. </script>
  35. </body>
  36. </html>

php代码

  1. <?php
  2. header("content-type:text/html;charset:utf-8");
  3. $callback=$_GET['jsonp'];
  4. $id=$_GET['id'];
  5. //模仿接口,根据查询条件返回不同的内容
  6. $users=[
  7. '{"name":"admin","email":"admin@php.cn"}',
  8. '{"name":"xiaofei","email":"xiaofei@php.cn"}',
  9. '{"name":"mike","email":"mike@php.cn"}',
  10. ];
  11. if (array_key_exists(($id-1),$users)){
  12. $user=$users[$id-1];
  13. }
  14. $json='{
  15. "title":"用户信息表",
  16. "user": '.$user. '
  17. }';
  18. echo $callback.'('.json_encode($json).')';

效果:

总结:这种接口请求应用的应该挺多的吧 像那种音乐接口,视频接口,包括短信接口等等

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:目前跨域的主流技术就是jsonp, 这是服务器无法拒绝的
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