ajax跨域访问实例

ajax实现跨域访问,用php写代理页面. 服务端处理程序:statistic.php

 

<? php 
session_start ();
require_once ( ' ./includes/common.php ' );
Header ( " Content-type:text/html;charset=GBK " ); // 加入头,防止乱码

/*
$referUrl=$_SERVER["HTTP_REFERER"];//取得上一页面地址
$referHost=$_SERVER["HTTP_HOST"];//取得当前主机名

$referFile=explode("/",$referUrl);//取得上一前面的主机名$referFile[2]

if($referFile[2]!=$referHost)//如果上一页面与本服务端程序不在同一主机则禁止执行
{
    echo "服务端禁止执行";
    exit();
}
*/
// 判断查询类型 1查询发展数量 2每日发短信数量,电话数量,时长 3每日登录时长在线时长
//$qry="3";

$Starttime   =   $dt . '  00:00:00 '  ;
$Endtime   =   $edt . '  23:59:59 ' ;


$SQL = " select count(*) as cnt from tb_user  where registtime between '$Starttime' and '$Endtime' " ;
$result = $db -> query( $SQL );
$row = $db -> fetch_array( $result );
$totalput = $row [ ' cnt ' ];
echo   " <TABLE width=800 border=0 align=center cellPadding=5 cellSpacing=1 bgcolor='#CCCCCC'> " ;
echo   " <TR bgcolor='#ffffff'><TD width=141>发展帐号  " . $totalput . "  个</TD></TR> " ;
echo   " </TABLE> " ;
$SQL = " select concat(telareacode,'-',telcode) as telcode,username,sum(case serviceid when '01' then fee else 0 end) as telmoney,sum(case serviceid when '02' then fee else 0 end) as smmoney from v_tel_sm_cdruser where starttime > '$Starttime' and endtime < '$Endtime' group by telcode,username UNION select '费用合计:','-',sum(case serviceid when '01' then fee else 0 end) as telmoney,sum(case serviceid when '02' then fee else 0 end) as smmoney from v_tel_sm_cdruser where starttime > '$Starttime' and endtime < '$Endtime' " ;
$result = $db -> query( $SQL );
$cur_num = $db -> num_rows( $result );
for  ( $i = 0 ; $i < $cur_num ; $i ++ )
{
    
$row = $db -> fetch_array( $result );
    
$telcode [ $i ] = HtmlEncode( $row [ ' telcode ' ]);
    
$username [ $i ] = HtmlEncode( $row [ ' username ' ]);
    
$telmoney [ $i ] = HtmlEncode( $row [ ' telmoney ' ]);
    
$smmoney [ $i ] = HtmlEncode( $row [ ' smmoney ' ]);
}
echo   " <br/> " ;
echo   " <TABLE width=800 border=0 align=center cellPadding=5 cellSpacing=1 bgcolor='#CCCCCC'> " ;
echo   " <TR bgcolor='#ffffff'><TD width='25%' align='center'>电话号码</TD><TD width='25%' align='center'>帐号名称 </TD><TD width='25%' align='center'>电话费用</TD><TD width='25%' align='center'>短信费用</TD></TR> " ;
for  ( $i = 0 ; $i < $cur_num ; $i ++ )
{
    
echo   " <TR bgcolor='#ffffff'><TD width='25%'> $telcode[$i]</TD><TD width='25%'> $username[$i]</TD><TD width='25%'> $telmoney[$i]</TD><TD width='25%'> $smmoney[$i]</TD></TR> " ;
}
echo   " </TABLE> " ;
$SQL = " select useraccount,sum(ollength) ollength,sum(loginlength) loginlength,sum(callintimes) callintimes,sum(callinlength) callinlength,sum(calltotimes) calltotimes,sum(calltolength) calltolength,sum(smouttimes) smouttimes,sum(smoutlength) smoutlength,sum(smintimes) smintimes,sum(sminlength) sminlength from hrb_report where reporttime between '$Starttime' and '$Endtime' group by useraccount union select '总计',sum(ollength) ollength,sum(loginlength) loginlength,sum(callintimes) callintimes,sum(callinlength) callinlength,sum(calltotimes) calltotimes,sum(calltolength) calltolength,sum(smouttimes) smouttimes,sum(smoutlength) smoutlength,sum(smintimes) smintimes,sum(sminlength) sminlength from hrb_report where reporttime between '$Starttime' and '$Endtime' " ;
$result = $db -> query( $SQL );
$cur_num = $db -> num_rows( $result );
for  ( $i = 0 ; $i < $cur_num ; $i ++ )
{
    
$row = $db -> fetch_array( $result );
    
$useraccount [ $i ] = HtmlEncode( $row [ ' useraccount ' ]);
    
$ollength [ $i ] = HtmlEncode( $row [ ' ollength ' ]);
    
$loginlength [ $i ] = HtmlEncode( $row [ ' loginlength ' ]);
    
$callintimes [ $i ] = HtmlEncode( $row [ ' callintimes ' ]);
    
$callinlength [ $i ] = HtmlEncode( $row [ ' callinlength ' ]);
    
$calltotimes [ $i ] = HtmlEncode( $row [ ' calltotimes ' ]);
    
$calltolength [ $i ] = HtmlEncode( $row [ ' calltolength ' ]);
    
$smouttimes [ $i ] = HtmlEncode( $row [ ' smouttimes ' ]);
    
$smoutlength [ $i ] = HtmlEncode( $row [ ' smoutlength ' ]);
    
$smintimes [ $i ] = HtmlEncode( $row [ ' smintimes ' ]);
    
$sminlength [ $i ] = HtmlEncode( $row [ ' sminlength ' ]);
}
echo   " <br/> " ;
echo   " <TABLE width=800 border=0 align=center cellPadding=5 cellSpacing=1 bgcolor='#CCCCCC'> " ;
echo   " <TR bgcolor='#ffffff'><TD width='10%' align='center'> 帐号<br/>名称</TD><TD width='9%' align='center'> 在线<br/>时长</TD><TD width='9%' align='center'> 登录<br/>时长</TD><TD width='9%' align='center'> 来电<br/>次数</TD><TD width='9%' align='center'> 来电<br/>时长</TD><TD width='9%' align='center'> 去电<br/>次数</TD><TD width='9%' align='center'> 去电<br/>时长</TD><TD width='9%' align='center'> 发短信<br/>次数</TD><TD width='9%' align='center'> 发短信<br/>时长</TD><TD width='9%' align='center'> 收短信<br/>次数</TD><TD width='9%' align='center'> 收短信<br/>时长</TD></TR> " ;

for  ( $i = 0 ; $i < $cur_num ; $i ++ )
{
    
echo   " <TR bgcolor='#ffffff'><TD width='10%' align='center'>$useraccount[$i]</TD><TD width='9%' align='center'>$ollength[$i]</TD><TD width='9%' align='center'>$loginlength[$i]</TD><TD width='9%' align='center'>$callintimes[$i]</TD><TD width='9%' align='center'>$callinlength[$i]</TD><TD width='9%' align='center'>$calltotimes[$i]</TD><TD width='9%' align='center'>$calltolength[$i]</TD><TD width='9%' align='center'>$smouttimes[$i]</TD><TD width='9%' align='center'>$smoutlength[$i]</TD><TD width='9%' align='center'>$smintimes[$i]</TD><TD width='9%' align='center'>$sminlength[$i]</TD></TR> " ;
}
echo   " </TABLE> " ;
/*
switch($qry)
{
    case "1":
    {    
        $SQL="select count(*) as cnt from tb_user  where registtime between '$Starttime' and '$Endtime'";
        $result=$db->query($SQL);
        $row=$db->fetch_array($result);
        $totalput=$row['cnt'];
        //echo $Starttime;
        echo "<TABLE width=800 border=0 align=center cellPadding=5 cellSpacing=1 bgcolor='#CCCCCC'>";
        echo "<TR bgcolor='#ffffff'><TD width=141>发展帐号 ".$totalput." 个</TD></TR>";
        echo "</TABLE>";
        break;
    }
    case "2":
    {    
        $SQL="select concat(telareacode,'-',telcode) as telcode,username,sum(case serviceid when '01' then fee else 0 end) as telmoney,sum(case serviceid when '02' then fee else 0 end) as smmoney from v_tel_sm_cdruser where starttime > '$Starttime' and endtime < '$Endtime' group by telcode,username UNION select '费用合计:','-',sum(case serviceid when '01' then fee else 0 end) as telmoney,sum(case serviceid when '02' then fee else 0 end) as smmoney from v_tel_sm_cdruser where starttime > '$Starttime' and endtime < '$Endtime'";
        $result=$db->query($SQL);
        $cur_num=$db->num_rows($result);
        for ($i=0;$i<$cur_num;$i++)
        {
            $row=$db->fetch_array($result);
            $telcode[$i]=HtmlEncode($row['telcode']);
            $username[$i]=HtmlEncode($row['username']);
            $telmoney[$i]=HtmlEncode($row['telmoney']);
            $smmoney[$i]=HtmlEncode($row['smmoney']);
        }
        echo "<TABLE width=800 border=0 align=center cellPadding=5 cellSpacing=1 bgcolor='#CCCCCC'>";
        echo "<TR bgcolor='#ffffff'><TD width='25%' align='center'>电话号码</TD><TD width='25%' align='center'>帐号名称 </TD><TD width='25%' align='center'>电话费用</TD><TD width='25%' align='center'>短信费用</TD></TR>";
        for ($i=0;$i<$cur_num;$i++)
        {
            echo "<TR bgcolor='#ffffff'><TD width='25%'> $telcode[$i]</TD><TD width='25%'> $username[$i]</TD><TD width='25%'> $telmoney[$i]</TD><TD width='25%'> $smmoney[$i]</TD></TR>";
        }
        echo "</TABLE>";
        break;
    }
    
    case "3":
    {    
        $SQL="select useraccount,sum(ollength) ollength,sum(loginlength) loginlength,sum(callintimes) callintimes,sum(callinlength) callinlength,sum(calltotimes) calltotimes,sum(calltolength) calltolength,sum(smouttimes) smouttimes,sum(smoutlength) smoutlength,sum(smintimes) smintimes,sum(sminlength) sminlength from hrb_report where reporttime between '$Starttime' and '$Endtime' group by useraccount union select '总计',sum(ollength) ollength,sum(loginlength) loginlength,sum(callintimes) callintimes,sum(callinlength) callinlength,sum(calltotimes) calltotimes,sum(calltolength) calltolength,sum(smouttimes) smouttimes,sum(smoutlength) smoutlength,sum(smintimes) smintimes,sum(sminlength) sminlength from hrb_report where reporttime between '$Starttime' and '$Endtime'";
        $result=$db->query($SQL);
        $cur_num=$db->num_rows($result);
        for ($i=0;$i<$cur_num;$i++)
        {
            $row=$db->fetch_array($result);
            $useraccount[$i]=HtmlEncode($row['useraccount']);
            $ollength[$i]=HtmlEncode($row['ollength']);
            $loginlength[$i]=HtmlEncode($row['loginlength']);
            $callintimes[$i]=HtmlEncode($row['callintimes']);
            $callinlength[$i]=HtmlEncode($row['callinlength']);
            $calltotimes[$i]=HtmlEncode($row['calltotimes']);
            $calltolength[$i]=HtmlEncode($row['calltolength']);
            $smouttimes[$i]=HtmlEncode($row['smouttimes']);
            $smoutlength[$i]=HtmlEncode($row['smoutlength']);
            $smintimes[$i]=HtmlEncode($row['smintimes']);
            $sminlength[$i]=HtmlEncode($row['sminlength']);
        }
        echo "<TABLE width=800 border=0 align=center cellPadding=5 cellSpacing=1 bgcolor='#CCCCCC'>";
        echo "<TR bgcolor='#ffffff'><TD width='10%' align='center'> 帐号名称</TD><TD width='9%' align='center'> 在线时长</TD><TD width='9%' align='center'> 登录时长</TD><TD width='9%' align='center'> 来电次数</TD><TD width='9%' align='center'> 来电时长</TD><TD width='9%' align='center'> 去电次数</TD><TD width='9%' align='center'> 去电时长</TD><TD width='9%' align='center'> 发短信次数</TD><TD width='9%' align='center'> 发短信时长</TD><TD width='9%' align='center'> 收短信次数</TD><TD width='9%' align='center'> 收短信时长</TD></TR>";
        
        for ($i=0;$i<$cur_num;$i++)
        {
            echo "<TR bgcolor='#ffffff'><TD width='10%' align='center'>$useraccount[$i]</TD><TD width='9%' align='center'>$ollength[$i]</TD><TD width='9%' align='center'>$loginlength[$i]</TD><TD width='9%' align='center'>$callintimes[$i]</TD><TD width='9%' align='center'>$callinlength[$i]</TD><TD width='9%' align='center'>$calltotimes[$i]</TD><TD width='9%' align='center'>$calltolength[$i]</TD><TD width='9%' align='center'>$smouttimes[$i]</TD><TD width='9%' align='center'>$smoutlength[$i]</TD><TD width='9%' align='center'>$smintimes[$i]</TD><TD width='9%' align='center'>$sminlength[$i]</TD></TR>";
        }
        echo "</TABLE>";
        break;
    }
 }
*/

?>

 

代理页面名称:AJAXProxy.php 源码:

 

<? php
require_once ( ' ./includes/common.php ' );
Header ( " Content-type:text/html;charset=GBK " ); // 加入头,防止乱码
header ( " Expires:   Mon,   26   Jul   1997   05:00:00   GMT " );          //    Date   in   the   past   
header ( " Last-Modified:    "     .     gmdate ( " D,   d   M   Y   H:i:s " )    .     "    GMT " );    //    always   modified   
header ( " Cache-Control:   no-cache,   must-revalidate " );      //    HTTP/1.1   
header ( " Pragma:   no-cache " );                                                      //    HTTP/1.0 
$http   =   new  COM( " MSXML2.XMLHTTP " ); 
switch ( $lnk )
    { 
        
case   1 :   $url = " http://nxdx.crm.cn/admin/statistic.php?dt=$dt&edt=$edt " ; break ;
        
case   2 :   $url = " http://gzdx.crm.cn/admin/statistic.php?dt=$dt&edt=$edt " break ;
        
case   3 :   $url = " http://hljcnc.dadianhua.com:8080/statistic.php?dt=$dt&edt=$edt " break ;
        
default :   $url = " xxx " ;
    }
$http -> open( " GET " , $url , false ); 
$http -> send(); 
echo   $http -> responseText;


?>

javascript代码页:

 

  var  http_request  =   false ;
 
function  send_request(url) 
 
{
    
//初始化、指定处理函数、发送请求的函数
    http_request = false;
    
//开始初始化XMLHttpRequest对象
    if(window.XMLHttpRequest) 
        

            
//Mozilla 浏览器
            http_request = new XMLHttpRequest();
            
if (http_request.overrideMimeType) 
            
{
                
//设置MiME类别
                http_request.overrideMimeType("text/xml");
            }

        }

    
else if (window.ActiveXObject) 
        
{
            
// IE浏览器
            try 
                
{
                    http_request 
= new ActiveXObject("Msxml2.XMLHTTP");
                }
 
            
catch (e) 
                
{
                    
try 
                    
{
                        http_request 
= new ActiveXObject("Microsoft.XMLHTTP");
                    }
 
                    
catch (e2) 
                    
{
                        http_request 
= false;
                    }

                }

        }

    
if (!http_request) 
        
// 异常,创建对象实例失败
            window.alert("不能创建XMLHttpRequest对象实例.");
            
return false;
        }

    http_request.onreadystatechange 
=processRequest;
    
// 确定发送请求的方式和URL以及是否同步执行下段代码
    try
     
{
        http_request.open(
"get",url, true);
     }
catch(e)
     
{
         alert(
'false');
     }

    http_request.send(
null);
 }


function  processRequest() 
{//
      if (http_request.readyState == 4
    
{
        
// 判断对象状态
        if (http_request.status == 200
        
// 信息已经成功返回,开始处理信息
        var rpt=http_request.responseText;
        document.getElementById(
"cnt").innerHTML=rpt;
        
//alert(rpt);
        }
 
        
else 
        
//页面不正常
            alert("您所请求的页面有异常。");
        }

    }
else 
    
{//只要未读取完成
        document.getElementById("cnt").innerHTML="正在读取数据中……";
    }
   
}


function  loadExternal(lnk) 
{
    
//var vl=document.getElementById("acnt").value;
  //载入扩展属性
  send_request("AJAXProxy.php?lnk="+lnk+"&dt="+document.getElementById("fromdate").value+"&edt="+document.getElementById("enddate").value);
  
/*
    switch(lnk){
        case "1":  send_request("http://nxdx.crm.cn/admin/statistic.php?dt="+document.getElementById("fromdate").value+"&edt="+document.getElementById("enddate").value);break;
        case "2":  send_request("http://gzdx.crm.cn/admin/statistic.php?dt="+document.getElementById("fromdate").value+"&edt="+document.getElementById("enddate").value);break;//服务端处理程序,操作函数;//选择操作函数
        case "3":  send_request("http://hljcnc.dadianhua.com:8080/statistic.php?dt="+document.getElementById("fromdate").value+"&edt="+document.getElementById("enddate").value);break;
        
  }
 
*/

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值