Blogger Information
Blog 36
fans 0
comment 0
visits 28106
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
CORS跨域请求与JSONP跨域请求
phpcn_u202398
Original
712 people have browsed it

1、CORS跨域请求

代码示例

前端

  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>跨域请求</button>
  10. <h3 style="color: blueviolet;"></h3>
  11. <script>
  12. var btn = document.querySelector("button");
  13. btn.onclick = function(){
  14. var xhr = new XMLHttpRequest();
  15. xhr.onreadystatechange = function(){
  16. if(xhr.readyState === 4 && xhr.status === 200){
  17. console.log(xhr.responseText);
  18. document.querySelector("h3").innerHTML=xhr.responseText;
  19. }
  20. };
  21. xhr.open("get","http://www.qwe.demo/test.php",true);
  22. xhr.send(null);
  23. }
  24. </script>
  25. </body>
  26. </html>

后端

  1. <?php
  2. header('Access-Control-Allow-Origin:http://www.php11.demo');
  3. echo "跨域脚本返回的数据";
  4. flush();
  5. ?>

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. function handle(jsonData){
  12. console.log(jsonData);
  13. var data = JSON.parse(jsonData);
  14. console.log(data);
  15. var ul = document.createElement('ul');
  16. ul.innerHTML += "<li>" + data.title + "</li>";
  17. ul.innerHTML += "<li>姓名:" + data.user.name + "</li>";
  18. ul.innerHTML += "<li>邮箱:" + data.user.email + "</li>";
  19. document.body.appendChild(ul);
  20. }
  21. var btn = document.querySelector("button");
  22. btn.onclick = function(){
  23. var script = document.createElement('script');
  24. script.src = "http://www.qwe.demo/test1.php?jsonp=handle&id=1";
  25. document.head.appendChild(script);
  26. }
  27. </script>
  28. </body>
  29. </html>
  30. <script>
  31. </script>

后端

  1. <?php
  2. header('content-type:text/html;charset=utf-8');
  3. $callback = $_GET['jsonp'];
  4. $id = $_GET['id'];
  5. $users = [
  6. 0=>'{"name":"zcx","email":"zcx@qq.com"}',
  7. 1=>'{"name":"zxc","email":"zxc@qq.com"}',
  8. 2=>'{"name":"cxz","email":"cxz@qq.com"}'
  9. ];
  10. if(array_key_exists(($id-1),$users)){
  11. $user = $users[$id-1];
  12. }
  13. $json = '{
  14. "title":"用户信息",
  15. "user": '.$user.'
  16. }';
  17. echo $callback .'('.json_encode($json).')';
  18. ?>

学习总结

本节课我们学习了CORS跨域请求与JSONP跨域请求的知识,通过本节课的学习使我知道如何进行跨域请求,希望通过后续的学习应用于实际开发中。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:实际项目中会有一些封装好的跨域方法可以用, 更方便, 但理解原理会更好掌握它
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