加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

jsonp跨域技术【php技术】

发布时间:2020-12-16 19:23:39 所属栏目:百科 来源:网络整理
导读:html代码 html head meta http-equiv = "content-type" content = "text/html;charset=utf-8" title jsonp跨域技术 / title style type = "text/css" #h1{ text-align:center; } #div{ margin:0auto; width:300px; height:200px; background:#ccc; } button{

html代码

 
 
  1. <html>
  2. <head>
  3. <metahttp-equiv="content-type"content="text/html;charset=utf-8">
  4. <title>jsonp跨域技术</title>
  5. <styletype="text/css">
  6. #h1{
  7. text-align:center;
  8. }
  9. #div{
  10. margin:0auto;
  11. width:300px;
  12. height:200px;
  13. background:#ccc;
  14. }
  15. button{
  16. display:block;
  17. margin:0auto;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <h1id='h1'>jsonp跨域技术</h1>
  23. <divid='div'>
  24. </div>
  25. <h1id='h1'><script>document.write(newDate())</script></h1>
  26. <buttonid="bid"name="button"onclick="fun()">jsonp请求数据</button>
  27. </body>
  28. <script>
  29. //实现js与php发生数据交互的一种技术,js通过请求php脚本,然后得到响应数据,最核心的作用是实现js跨域,跨域就是跨主机或者跨服务器
  30. //为什么要用jsonp技术,是因为ajax技术无法跨域
  31. functionfasong3(data){
  32. //alert(data.name);//弹出
  33. //alert(data.age);
  34. varstr="";
  35. str+="name:"+data.name+"<br>";
  36. str+="age:"+data.age+"<br>";
  37. document.getElementById('div').innerHTML=str;
  38. }
  39. functionfun(){
  40. varobj=document.createElement("script");//创建标签
  41. obj.src="http://192.168.11.188/index111.php?cb=fasong3";//请求地址
  42. document.body.appendChild(obj);//绑定子类
  43. }
  44. </script>
  45. </html>

http://192.168.11.188/服务器地址代码

 
 
  1. <?php
  2. $fasong3=$_GET["cb"];
  3. //echo"<script>$fasong3('2222211')</script>";
  4. $str='{"name":"nihao","age":"30"}';
  5. echo$fasong3.'('.$str.')';

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读