网站建设
  简约型网页设计套餐998
  实惠型网站建设套餐2580
  综合型网站制作套餐4980
  网站改版与网站维护
  行业网站建设方案
  大型网站建设解决方案
  企业网站建设流程
  帝网科技网站设计与网站制作
建站FAQ
·网站空间问题解答
·企业邮箱问题解答
 
酷站欣赏
·房产酷站(379)
·综合门户(8 9)
·建筑装饰(603)
·手机通讯(354)
·生活购物(376)
·医疗保健(199)
·文化摄影(602)
·休闲体育(399)
>>更多酷站欣赏
网站优化
·Google(谷歌)优化   ·百度(BaiDu)优化
·雅虎(Yahoo)优化    ·Alexa排名优化   
·Google AdSense   ·DMOZ目录提交  
建站知识
·网站建设知识·网站名词解释·网站运营知识
·网络营销知识·搜索引擎知识·实用技术文摘
网站推广
百度网站推广 google网站推广
搜狐网站推广 网易网站推广
新浪网站推广   雅虎网站推广
  您当前位置: 当前位置:帝网科技 >> web开发 >> PHP专栏 >> 浏览文章
 
 
PHP中使用ASP.NET AJAX
作者:袁袁整理 来源:帝网科技 日期:2008年10月27日 点击数:


借助于CodePlex上开源项目PHP for Microsoft AJAX Library的帮助,我们已经可以在PHP上使用ASP.NET AJAX的很多核心功能了。
下载安装

PHP for Microsoft AJAX Library目前仅仅处于Alpha阶段,想实际使用似乎还早了点,只能尝鲜了。

预先需求有PHP 5.2版本,且必须安装了php-json模块。

安装方法:

下载PHP for Microsoft AJAX Library并解压缩
下载Microsoft AJAX Library(http://ajax.asp.net)
在PHP Web Service代码中include一下MSAjaxService.php。
在调用该Web Service的页面中,引入MicrosoftAjax.js文件。
下面来看一个“经典”的场景:调用服务器端方法取得复杂类型。

 编写Service文件

新建一个php文件,命名为EmployeeService.php。首先写上这一句,include必要的支持代码:

require_once 'MSAjaxService.php';

然后定义一个Employee类。四个属性一目了然,不用多说:

class Employee{    public $Id;    public $Name;    public $Email;    public $Salary;     function __construct($id, $name, $email, $salary)    {        $this->Id = $id;        $this->Name = $name;        $this->Email = $email;        $this->Salary= $salary;    }}

接下来是EmployeeService类,继承与MSAjaxService.php中的MSAjaxService基类。其中定义一个方法,用来返回一个Employee对象:

class EmployeeService extends MSAjaxService{    function GetEmployee()    {return new Employee(12345, "Dflying Chen", "Dflying@some.com", 1000);    }}

然后新建一个EmployeeService的实例,并且调用基类的ProcessRequest()方法,处理该请求:

$theService = new EmployeeService();$theService->ProcessRequest();

大功告成!

 编写调用页面

新建一个页面,php或者html均可——程序比较简单。这回我们没了scriptManager的帮助,引入ASP.NET AJAX客户端脚本文件以及上面的这个Service只能靠手工了。注意EmployeeService.php/js可以得到该Service的客户端代理,和ASP.NET平台上的语法一样:

    ASP.NET AJAX On PHP Demotitle><script type="text/javascript" src="MicrosoftAjaxLibrary/MicrosoftAjax.js"><script>    "text/javascript" src="EmployeeService.php/js">script>head> </p><p>程序的UI部分很简单,按钮用来触发异步调用,</p><p>用来显示调用结果: </p><p> <body>    <input id="btnGetEmployee" type="button" value="Get an Employee" onclick="return btnGetEmployee_onclick()" />    <div id="resultDiv">    div>body> </p><p>在该按钮的click事件处理函数中,调用该Service,语法也和ASP.NET AJAX中一致,非常方便:</p><p>function btnGetEmployee_onclick(){    EmployeeService.GetEmployee(onSucceeded);} </p><p>在回调函数中,把得到的Employee对象显示到resultDiv中:</p><p>function onSucceeded(result){ var sb = new Sys.StringBuilder("Server returns an Employee object: ");    sb.append("Id: " + result.Id + "");    sb.append("Name: " + result.Name + "");    sb.append("Email: " + result.Email + "");    sb.append("Salary: " + result.Salary + "");     $get("resultDiv").innerHTML = sb.toString();} </p><p>大功告成!</p><p> 示例程序界面</p><p>第一次访问</p><p><img src="/UpFiles/BeyondPic/200902/20090217185643341.jpg" width="362" border="0" / style="cursor: pointer" alt="点此在新窗口浏览图片" onclick="javascript:window.open(this.src);" onmousewheel="return bbimg(this)" onload="javascript:resizepic(this)" border="0"/> </p><p><br />点击Get an Employee按钮后</p><p> </p><p><img src="/UpFiles/BeyondPic/200902/20090217185643490.jpg" width="362" border="0" / style="cursor: pointer" alt="点此在新窗口浏览图片" onclick="javascript:window.open(this.src);" onmousewheel="return bbimg(this)" onload="javascript:resizepic(this)" border="0"/> </p> </p> <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class=box> <tr> <td height="30" colspan="3" bgcolor="#E8EBDA"><strong>  相关文章</strong></td> </tr> <tr> <td width="8"> </td> <td height="180"><table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr> <td width="50%" height="22"> <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0"> <tr><td> ·<a href="http://www.dwwin.com/web/php/9848956.html" title="如何在PHP开启gzip页面压缩实例正文分析错误">如何在PHP开启gzip页面压缩实例正文分</a></td></tr> </table> </td> <td width="50%" height="22"> <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0"> <tr><td> ·<a href="http://www.dwwin.com/web/php/82921515652.html" title="如何正确运用PHP json_encode函数进行中文转换正文分析错误">如何正确运用PHP json_encode函数进行</a></td></tr> </table> </td> </tr> <tr> <td width="50%" height="22"> <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0"> <tr><td> ·<a href="http://www.dwwin.com/web/php/76271840.html" title="PHP下载断点续传的代码正文分析错误">PHP下载断点续传的代码正文分析错误</a></td></tr> </table> </td> <td width="50%" height="22"> <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0"> <tr><td> ·<a href="http://www.dwwin.com/web/php/96825146.html" title="PHP取得客户端IP地址代码正文分析错误">PHP取得客户端IP地址代码正文分析错误</a></td></tr> </table> </td> </tr> <tr> <td width="50%" height="22"> <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0"> <tr><td> ·<a href="http://www.dwwin.com/web/php/678459381211.html" title="PHP判断用户IP来路的一个方法正文分析错误">PHP判断用户IP来路的一个方法正文分析</a></td></tr> </table> </td> <td width="50%" height="22"> <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0"> <tr><td> ·<a href="http://www.dwwin.com/web/php/50249.html" title="PHP+mysql分页代码正文分析错误">PHP+mysql分页代码正文分析错误</a></td></tr> </table> </td> </tr> <tr> <td width="50%" height="22"> <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0"> <tr><td> ·<a href="http://www.dwwin.com/web/php/355834576.html" title="php去除HTML标记正文分析错误">php去除HTML标记正文分析错误</a></td></tr> </table> </td> <td width="50%" height="22"> <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0"> <tr><td> ·<a href="http://www.dwwin.com/web/php/94382.html" title="php轻松快速缓存全站正文分析错误">php轻松快速缓存全站正文分析错误</a></td></tr> </table> </td> </tr> <tr> <td width="50%" height="22"> <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0"> <tr><td> ·<a href="http://www.dwwin.com/web/php/0435351.html" title="php多语言网站解决方案正文分析错误">php多语言网站解决方案正文分析错误</a></td></tr> </table> </td> <td width="50%" height="22"> <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0"> <tr><td> ·<a href="http://www.dwwin.com/web/php/542851642.html" title="php图片验证码函数正文分析错误">php图片验证码函数正文分析错误</a></td></tr> </table> </td> </tr> <tr> <td width="50%" height="22"> <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0"> <tr><td> ·<a href="http://www.dwwin.com/web/php/3179998.html" title="PHP初学者常见问题集(21问答)正文分析错误">PHP初学者常见问题集(21问答)正文分</a></td></tr> </table> </td> <td width="50%" height="22"> <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0"> <tr><td> ·<a href="http://www.dwwin.com/web/php/3971826.html" title="分享PHP技术开发技巧正文分析错误">分享PHP技术开发技巧正文分析错误</a></td></tr> </table> </td> </tr> <tr> <td width="50%" height="22"> <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0"> <tr><td> ·<a href="http://www.dwwin.com/web/php/384129.html" title="cURL库功能简介:抓取网页 POST数据及其他正文分析错误">cURL库功能简介:抓取网页 POST数据及</a></td></tr> </table> </td> <td width="50%" height="22"> <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0"> <tr><td> ·<a href="http://www.dwwin.com/web/php/99827477238047.html" title="PHP常用函数:过滤HTML字符串正文分析错误">PHP常用函数:过滤HTML字符串正文分析</a></td></tr> </table> </td> </tr> <tr> <td width="50%" height="22"> <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0"> <tr><td> ·<a href="http://www.dwwin.com/web/php/106534114809.html" title="php fscanf()函数使用方法详解正文分析错误">php fscanf()函数使用方法详解正文分析</a></td></tr> </table> </td> <td width="50%" height="22"> <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0"> <tr><td> ·<a href="http://www.dwwin.com/web/php/81654235826572.html" title="和php有关的几种常见安全详解正文分析错误">和php有关的几种常见安全详解正文分析</a></td></tr> </table> </td> </tr> <tr> <td width="50%" height="22"> <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0"> <tr><td> ·<a href="http://www.dwwin.com/web/php/1658972747167.html" title="php连接mysql出现乱码解决办法正文分析错误">php连接mysql出现乱码解决办法正文分析</a></td></tr> </table> </td> <td width="50%" height="22"> <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0"> <tr><td> ·<a href="http://www.dwwin.com/web/php/53011384.html" title="PHP非常简单的使用模板制作静态页面正文分析错误">PHP非常简单的使用模板制作静态页面正</a></td></tr> </table> </td> </tr> <tr> <td width="50%" height="22"> <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0"> <tr><td> ·<a href="http://www.dwwin.com/web/php/08985.html" title="非常简单PHP缩略图生成程序源代码正文分析错误">非常简单PHP缩略图生成程序源代码正文</a></td></tr> </table> </td> <td width="50%" height="22"> <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0"> <tr><td> ·<a href="http://www.dwwin.com/web/php/731054.html" title="无法载入 mcrypt 扩展,请检查 PHP 配置 终极解决方案正文分析错误">无法载入 mcrypt 扩展,请检查 PHP 配置</a></td></tr> </table> </td> </tr> </table> </td> <td width="8"> </td> </tr> </table> </td> </tr> <tr> <td height="35" align=middle><div style="TEXT-ALIGN: left"><strong>Tags:</strong><a href="/plus/search.asp?searchtype=5&channelid=1&tags=网站建设" target="_blank">网站建设</a> <a href="/plus/search.asp?searchtype=5&channelid=1&tags=网络编程" target="_blank">网络编程</a> <a href="/plus/search.asp?searchtype=5&channelid=1&tags=PHP编程" target="_blank">PHP编程</a> </div></td> </tr> <tr> <td vAlign=top align=middle height=1><table style="border-top:#CCCCCC 1px dashed;" cellSpacing=0 cellPadding=0 width="100%" border=0 > <tr> <td></td> </tr> </table></td> </tr> </table> </td> </tr> </table></td> </tr> </table> </td> </tr> </table> </td> </tr> <tr> <td height="10" valign="top"> </td> </tr> </table> <table width="998" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF"> <tr valign="bottom"> <td colspan="3"><div align="center"> <table width="998" border="0" align="center"> <tr> <td height="2" background="../../images/footbg.jpg"></td> </tr> </table> <p align="center" class="text_About"><A href="../../company_hj.html"><font class="text_About">公司环境</font></A> | <A href="../../cooperate.html"><font class="text_About">合作伙伴</font></A> | <A href="../../job.html"><font class="text_About">人才招聘</font></A> | <A href="../../payment.html"><font class="text_About">付款方式</font></A> | <A href="../../Aboutus.html"><font class="text_About">关于我们</font></A> <br> <div align="center" class="text_About"><strong>QQ即时沟通:</strong><strong></strong><a href="http://wpa.qq.com/msgrd?V=1&Uin=1103573484&Site=dwwin.com&Menu=yes" target="blank"><img border="0" src="http://wpa.qq.com/pa?p=1:1103573484:4" alt="点击这里给我发消息" onerror="src='images/QQonline.gif'" /><font class="text_About">1103573484 业务咨询</font></a>  <a href="http://wpa.qq.com/msgrd?V=1&Uin=1026191201&Site=dwwin.com&Menu=yes" target="blank"><img border="0" src="http://wpa.qq.com/pa?p=1:1026191201:4" alt="点击这里给我发消息" onerror="src='/images/online.gif'" /><font class="text_About">1026191201 客户服务</font></a>  <a href="http://wpa.qq.com/msgrd?V=1&Uin=2272519115&Site=dwwin.com&Menu=yes" target="blank"><img border="0" src="http://wpa.qq.com/pa?p=1:2272519115:4" alt="点击这里给我发消息" onerror="src='/images/online.gif'" /><font class="text_About">2272519115 技术支持</font></a>  <a href="http://wpa.qq.com/msgrd?V=1&Uin=65229124&Site=dwwin.com&Menu=yes" target="blank"><img border="0" src="http://wpa.qq.com/pa?p=1:65229124:4" alt="点击这里给我发消息" onerror="src='/images/online.gif'" /><font class="text_About">65229124 技术支持</font></a> </div> </div></td> </tr> <tr> <td width="146"><div align="center"></div></td> <td width="696" valign="top"><div align="center" class="text_About">地址:广州市天河区中山大道中120号D805 电话:020-82529556 传真:020-82529556<br> 广州帝网网络科技有限公司 版权所有 粤ICP备08119341号</div></td> <td width="146"><a href=http://www.cdnet110.com/alert/ target=_blank rel=nofollow><img src=../../images/gzwd.gif width=40 height=53 border=0></a><img src=../../images/icp.gif width=35 height=43> </td> </tr> </table> <script src='http://www.dwwin.com/new_qq.js' language='JavaScript' charset='gb2312'></script> <div style="display:none;"> <script src='http://s11.cnzz.com/stat.php?id=1194792&web_id=1194792' language='JavaScript' charset='gb2312'></script> </div> </BODY></HTML> <script src="http://www.dwwin.com/ks_inc/ajax.js"></script>