jsonp跨域技術【php技術】

 html代碼

  1. <html> 
  2.     <head> 
  3.         <meta http-equiv="content-type" content="text/html;charset=utf-8"> 
  4.         <title>jsonp跨域技術</title> 
  5.         <style type="text/css"> 
  6.             #h1{ 
  7.                 text-align:center; 
  8.             } 
  9.             #div{ 
  10.                 margin:0 auto; 
  11.                 width:300px; 
  12.                 height:200px; 
  13.                 background:#ccc;     
  14.             } 
  15.             button{ 
  16.                 display:block; 
  17.                 margin:0 auto; 
  18.             } 
  19.         </style> 
  20.     </head> 
  21.     <body> 
  22.         <h1 id='h1'>jsonp跨域技術</h1> 
  23.         <div id='div'> 
  24.          
  25.         </div> 
  26.         <h1 id='h1'><script>document.write(new Date())</script></h1> 
  27.          
  28.         <button id="bid" name="button" onclick="fun()">jsonp請求數據</button> 
  29.     </body> 
  30.     <script> 
  31.         //實現js與php發生數據交互的一種技術,js通過請求php腳本,然後得到響應數據,最核心的作用是實現js跨域,跨域就是跨主機或者跨服務器 
  32.  
  33.         //爲什麼要用jsonp技術,是因爲ajax技術無法跨域 
  34.         function fasong3(data){ 
  35.                 //alert(data.name);//彈出 
  36.                 //alert(data.age); 
  37.                 var str=""
  38.                 str+="name:"+data.name+"<br>"; 
  39.                 str+="age:"+data.age+"<br>"; 
  40.                 document.getElementById('div').innerHTML=str
  41.         } 
  42.         function fun(){ 
  43.             var obj=document.createElement("script");//創建標籤 
  44.             obj.src="http://192.168.11.188/index111.php?cb=fasong3";//請求地址 
  45.             document.body.appendChild(obj);//綁定子類 
  46.         } 
  47.          
  48.     </script> 
  49.  
  50. </html> 

http://192.168.11.188/服務器地址代碼

 

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章