注明:暂时不支持跨域名下GET请求
http://192.168.66.90:8080//php/POST_GET.php
PHP代码
- <?php
 - //目前暂不支持跨域GET请求
 - header('Content-type: text/json');
 - html_entity_decode($string, ENT_QUOTES, 'UTF-8');
 - //回调参数设置
 - $param="callback";
 - $callback=$_REQUEST[$param];
 - //判断请求参数就是同域名下AJAX请求
 - if(!isset($callback)){
 - //返回多个Id
 - if($_POST){
 - exit(json_encode($_POST));
 - }else if($_GET){
 - exit(json_encode($_GET));
 - }
 - //返回单个Id
 - if($_POST['Id']){
 - $a=$_POST['Id'];
 - //echo $a;
 - exit(json_encode($a));
 - }else if($_GET['Id']){
 - $a=$_GET['Id'];
 - //echo $a;
 - exit(json_encode($a));
 - }
 - //强制开关按钮
 - if($_POST['CT']){
 - $a=$_POST['CT'];
 - echo $a;
 - }else if($_GET['CT']){
 - $a=$_GET['CT'];
 - echo $a;
 - }
 - }
 - //判断请求参数存在就是实现JSONP跨越提交
 - if(isset($callback)){
 - //强制开关按钮
 - if($_POST['Id']){
 - $a=$_POST['Id'];
 - $b=json_encode($a);
 - $str=$callback."(".$b.")";
 - }else{
 - $a=$_POST['CT'];
 - $str=$callback."(".$a.")";
 - }
 - echo $str;
 - }
 - ?>
 
http://192.168.66.90:8080/Ajax/s6.html
XML/HTML代码
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 - <html xmlns="http://www.w3.org/1999/xhtml">
 - <head>
 - <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 - <title>ajax test</title>
 - <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
 - <script type="text/javascript">
 - $(document).ready(function(){
 - //点击请求http接口,删除多条数据
 - $(".delall").click(function(){
 - //拼写成JSON数据格式,提交http接口JSON不需要转字符串
 - //alert(JSON.stringify(obj))
 - var val=$(".dellink.on").map(function(){
 - return $(this).attr("id");
 - }).get();
 - var obj={Id:val};
 - //var vot=JSON.stringify(obj);
 - //alert(obj)
 - /***** 同域名下多条ID请求
 - $.ajax({
 - url:"/php/POST_GET.php",
 - type:"post",
 - dataType:"json",
 - //jsonp:"callback",
 - data:obj,
 - success:function(data){
 - //var yy=JSON.stringify(data);
 - //alert(yy)
 - $.each(data.Id,function(i,item){
 - $('#'+item).slideUp();
 - });
 - },
 - error:function(){
 - alert("异常!");
 - }
 - });
 - */
 - /***** 跨域名多条ID请求 *****/
 - $.ajax({
 - url:"http://192.168.66.90:8080//php/POST_GET.php",
 - type:"post",
 - dataType:"jsonp",
 - jsonp:"callback",
 - data:obj,
 - success:function(data){
 - //var yy=JSON.stringify(data);
 - //alert(data)
 - $.each(data,function(i,item){
 - $('#'+item).slideUp();
 - });
 - },
 - error:function(){
 - alert("异常!");
 - }
 - });
 - });
 - //点击请求http接口,删除单条数据
 - $(".dellink").click(function() {
 - var thisId=$(this).attr("id");
 - /***** 同域名Ajax请求
 - $.ajax({
 - url:"/php/POST_GET.php",
 - type:"post",
 - dataType:"json",
 - //jsonp:"callback",
 - data:{Id:thisId},
 - success:function(data){
 - //var yy=JSON.stringify(data);
 - //alert(yy)
 - $.each(data,function(i,item){
 - $('#'+item).slideUp();
 - });
 - },
 - error:function(){
 - alert("异常!");
 - }
 - });
 - *****/
 - /****** 可支持jsonp跨域名请求 */
 - $.ajax({
 - url:"http://192.168.66.90:8080//php/POST_GET.php",
 - type:"post",
 - dataType:"jsonp",
 - jsonp:"callback",
 - data:{Id:thisId},//{Id:"44554547"}
 - success:function(data){
 - var yy=JSON.stringify(data);
 - //alert(data)
 - if(thisId==data){
 - $('#'+data).slideUp();
 - }
 - },
 - error:function(){
 - alert("异常!");
 - }
 - });
 - });
 - //控制开关(服务器返回0或1)
 - $(".control").click(function(){
 - var hason=$(this).hasClass("on");
 - /***** 同域名下访问http接口
 - if(hason){
 - $.ajax({
 - url:"/php/POST_GET.php",
 - type:"post",
 - dataType:"json",
 - data:{CT:"0"},
 - success:function(data){
 - $(".control").removeClass("on");
 - $(".control").html("广告关闭")
 - },
 - error:function(){
 - alert("异常!");
 - }
 - });
 - }else{
 - $.ajax({
 - url:"/php/POST_GET.php",
 - type:"post",
 - dataType:"json",
 - data:{CT:"1"},
 - success:function(data){
 - $(".control").addClass("on");
 - $(".control").html("广告开启")
 - },
 - error:function(){
 - alert("异常!");
 - }
 - });
 - }
 - */
 - /***** 跨域名访问http接口 *****/
 - if(hason){
 - $.ajax({
 - url:"http://192.168.66.90:8080//php/POST_GET.php",
 - type:"post",
 - dataType:"jsonp",
 - jsonp:"callback",
 - data:{CT:"0"},
 - success:function(data){
 - if(data==0){
 - $(".control").removeClass("on");
 - $(".control").html("广告关闭")
 - }
 - },
 - error:function(){
 - alert("异常!");
 - }
 - });
 - }else{
 - $.ajax({
 - url:"http://192.168.66.90:8080//php/POST_GET.php",
 - type:"post",
 - dataType:"jsonp",
 - jsonp:"callback",
 - data:{CT:"1"},
 - success:function(data){
 - if(data==1){
 - $(".control").addClass("on");
 - $(".control").html("广告开启")
 - }
 - },
 - error:function(){
 - alert("异常!");
 - }
 - });
 - }
 - });
 - //control end
 - });
 - </script>
 - </head>
 - <body>
 - <style type="text/css">
 - body { font-family: 'Microsoft Yahei'; margin:0; padding:0; font-weight:normal}
 - .dellink,.delall,.list .control{height:60px; line-height:60px; color:#fff; width:600px; font-size:22px; text-align:center; margin:2px auto; cursor:pointer;}
 - .dellink{ display:block; background:#000;}
 - .delall{ background: #CC3366; }
 - .list .control{ background:#CC0033; }
 - .list .on{background:#006633;}
 - </style>
 - <a class="dellink on" id="1001">删除单条数据 0111</a>
 - <a class="dellink" id="1002">删除单条数据 022</a>
 - <a class="dellink on" id="1003">删除单条数据 0333</a>
 - <a class="dellink on" id="1004">删除单条数据 044</a>
 - <a class="dellink" id="1005">删除单条数据 0555</a>
 - <div class="delall">请求http接口【并删除多条数据】</div>
 - <div class="list">
 - <div class="control on">广告开启</div>
 - </div>
 - </body>
 - </html>
 
使用原生JS写Ajax与
JSTL 与 JSP 或
 

 2015/04/17 10:32 | by 
  
 
 
 
 
 


