注明:暂时不支持跨域名下GET请求

http://192.168.66.90:8080//php/POST_GET.php

PHP代码
  1. <?php  
  2. //目前暂不支持跨域GET请求  
  3. header('Content-type: text/json');  
  4. html_entity_decode($string, ENT_QUOTES, 'UTF-8');  
  5.   
  6. //回调参数设置  
  7. $param="callback";  
  8. $callback=$_REQUEST[$param];  
  9.   
  10. //判断请求参数就是同域名下AJAX请求  
  11. if(!isset($callback)){  
  12.       
  13.     //返回多个Id  
  14.     if($_POST){  
  15.         exit(json_encode($_POST));  
  16.     }else if($_GET){  
  17.         exit(json_encode($_GET));  
  18.     }  
  19.   
  20.     //返回单个Id  
  21.     if($_POST['Id']){  
  22.         $a=$_POST['Id'];  
  23.         //echo $a;  
  24.         exit(json_encode($a));  
  25.     }else if($_GET['Id']){  
  26.         $a=$_GET['Id'];  
  27.         //echo $a;  
  28.         exit(json_encode($a));  
  29.     }  
  30.       
  31.     //强制开关按钮  
  32.     if($_POST['CT']){  
  33.         $a=$_POST['CT'];  
  34.         echo $a;  
  35.     }else if($_GET['CT']){  
  36.         $a=$_GET['CT'];  
  37.         echo $a;  
  38.     }  
  39. }  
  40.   
  41. //判断请求参数存在就是实现JSONP跨越提交  
  42. if(isset($callback)){  
  43.       
  44.     //强制开关按钮  
  45.     if($_POST['Id']){  
  46.         $a=$_POST['Id'];  
  47.         $b=json_encode($a);  
  48.         $str=$callback."(".$b.")";  
  49.     }else{  
  50.         $a=$_POST['CT'];  
  51.         $str=$callback."(".$a.")";  
  52.     }  
  53.     echo $str;  
  54.   
  55. }  
  56.   
  57. ?>  

http://192.168.66.90:8080/Ajax/s6.html

XML/HTML代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>ajax test</title>  
  6. <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>    
  7. <script type="text/javascript">    
  8.   
  9. $(document).ready(function(){  
  10.   
  11. //点击请求http接口,删除多条数据  
  12. $(".delall").click(function(){  
  13.   
  14.     //拼写成JSON数据格式,提交http接口JSON不需要转字符串  
  15.     //alert(JSON.stringify(obj))  
  16.     var val=$(".dellink.on").map(function(){    
  17.         return $(this).attr("id");        
  18.     }).get();  
  19.     var obj={Id:val};  
  20.     //var vot=JSON.stringify(obj);  
  21.     //alert(obj)  
  22.       
  23.     /***** 同域名下多条ID请求  
  24.     $.ajax({  
  25.         url:"/php/POST_GET.php",  
  26.         type:"post",  
  27.         dataType:"json",  
  28.         //jsonp:"callback",  
  29.         data:obj,  
  30.         success:function(data){  
  31.             //var yy=JSON.stringify(data);  
  32.             //alert(yy)  
  33.               
  34.             $.each(data.Id,function(i,item){  
  35.                 $('#'+item).slideUp();  
  36.             });      
  37.         },  
  38.         error:function(){      
  39.             alert("异常!");  
  40.         }  
  41.     });  
  42.     */  
  43.       
  44.     /***** 跨域名多条ID请求 *****/  
  45.     $.ajax({  
  46.         url:"http://192.168.66.90:8080//php/POST_GET.php",  
  47.         type:"post",  
  48.         dataType:"jsonp",  
  49.         jsonp:"callback",  
  50.         data:obj,  
  51.         success:function(data){  
  52.             //var yy=JSON.stringify(data);  
  53.             //alert(data)  
  54.               
  55.             $.each(data,function(i,item){  
  56.                 $('#'+item).slideUp();  
  57.             });      
  58.         },  
  59.         error:function(){      
  60.             alert("异常!");  
  61.         }  
  62.     });  
  63. });  
  64.   
  65. //点击请求http接口,删除单条数据  
  66. $(".dellink").click(function() {    
  67.     var thisId=$(this).attr("id");  
  68.     /***** 同域名Ajax请求  
  69.     $.ajax({  
  70.         url:"/php/POST_GET.php",  
  71.         type:"post",  
  72.         dataType:"json",  
  73.         //jsonp:"callback",  
  74.         data:{Id:thisId},  
  75.         success:function(data){  
  76.             //var yy=JSON.stringify(data);  
  77.             //alert(yy)  
  78.               
  79.             $.each(data,function(i,item){  
  80.                 $('#'+item).slideUp();  
  81.             });  
  82.         },  
  83.         error:function(){      
  84.             alert("异常!");  
  85.         }  
  86.     });  
  87.     *****/  
  88.     /****** 可支持jsonp跨域名请求 */  
  89.     $.ajax({  
  90.         url:"http://192.168.66.90:8080//php/POST_GET.php",  
  91.         type:"post",  
  92.         dataType:"jsonp",  
  93.         jsonp:"callback",  
  94.         data:{Id:thisId},//{Id:"44554547"}  
  95.         success:function(data){  
  96.             var yy=JSON.stringify(data);  
  97.             //alert(data)  
  98.             if(thisId==data){  
  99.                 $('#'+data).slideUp();  
  100.             }  
  101.         },  
  102.         error:function(){      
  103.             alert("异常!");  
  104.         }  
  105.     });  
  106. });  
  107.   
  108.   
  109. //控制开关(服务器返回0或1)  
  110. $(".control").click(function(){  
  111.     var hason=$(this).hasClass("on");  
  112.     /***** 同域名下访问http接口  
  113.     if(hason){  
  114.         $.ajax({  
  115.             url:"/php/POST_GET.php",  
  116.             type:"post",  
  117.             dataType:"json",  
  118.             data:{CT:"0"},  
  119.             success:function(data){  
  120.                 $(".control").removeClass("on");  
  121.                 $(".control").html("广告关闭")  
  122.             },  
  123.             error:function(){      
  124.                 alert("异常!");  
  125.             }  
  126.         });  
  127.     }else{  
  128.         $.ajax({  
  129.             url:"/php/POST_GET.php",  
  130.             type:"post",  
  131.             dataType:"json",  
  132.             data:{CT:"1"},  
  133.             success:function(data){  
  134.                 $(".control").addClass("on");  
  135.                 $(".control").html("广告开启")  
  136.             },  
  137.             error:function(){      
  138.                 alert("异常!");  
  139.             }  
  140.         });  
  141.     }  
  142.     */    
  143.     /***** 跨域名访问http接口 *****/  
  144.     if(hason){  
  145.         $.ajax({  
  146.             url:"http://192.168.66.90:8080//php/POST_GET.php",  
  147.             type:"post",  
  148.             dataType:"jsonp",  
  149.             jsonp:"callback",  
  150.             data:{CT:"0"},  
  151.             success:function(data){  
  152.                 if(data==0){  
  153.                     $(".control").removeClass("on");  
  154.                     $(".control").html("广告关闭")  
  155.                 }  
  156.             },  
  157.             error:function(){      
  158.                 alert("异常!");  
  159.             }  
  160.         });  
  161.     }else{  
  162.         $.ajax({  
  163.             url:"http://192.168.66.90:8080//php/POST_GET.php",  
  164.             type:"post",  
  165.             dataType:"jsonp",  
  166.             jsonp:"callback",  
  167.             data:{CT:"1"},  
  168.             success:function(data){  
  169.                 if(data==1){  
  170.                     $(".control").addClass("on");  
  171.                     $(".control").html("广告开启")  
  172.                 }  
  173.             },  
  174.             error:function(){      
  175.                 alert("异常!");  
  176.             }  
  177.         });  
  178.     }  
  179.   
  180. });  
  181. //control end  
  182.   
  183. });  
  184. </script>  
  185. </head>  
  186. <body>    
  187. <style type="text/css">  
  188. body { font-family: 'Microsoft Yahei'; margin:0; padding:0; font-weight:normal}  
  189. .dellink,.delall,.list .control{height:60px; line-height:60px; color:#fff; width:600px; font-size:22px; text-align:center; margin:2px auto;  cursor:pointer;}  
  190. .dellink{ display:block; background:#000;}  
  191. .delall{  background: #CC3366; }  
  192. .list .control{ background:#CC0033; }  
  193. .list .on{background:#006633;}  
  194. </style>  
  195. <a class="dellink on" id="1001">删除单条数据 0111</a>  
  196. <a class="dellink" id="1002">删除单条数据 022</a>  
  197. <a class="dellink on" id="1003">删除单条数据 0333</a>  
  198. <a class="dellink on" id="1004">删除单条数据 044</a>  
  199. <a class="dellink" id="1005">删除单条数据 0555</a>  
  200. <div class="delall">请求http接口【并删除多条数据】</div>  
  201. <div class="list">  
  202. <div class="control on">广告开启</div>  
  203. </div>  
  204. </body>    
  205. </html>  

 

PHP/Java | 评论(0) | 引用(0) | 阅读(5666)