当前位置: 首页 > 图文教程 > 网络编程 > PHP > PHP中使用ASP.NET AJAX

PHP
PHP新手总结的PHP基础知识
php实现gb2312和unicode间编码转换
用php语言实现数据库连接详细代码介绍
详细解析 PHP 向 MySQL 发送数据过程
利用PHP V5开发多任务应用程序
详细讲解PHP中缓存技术的应用
php escapeshellcmd多字节编码漏洞
《PHP设计模式介绍》导言
《PHP设计模式介绍》第一章 编程惯用法
《PHP设计模式介绍》第二章 值对象模式
《PHP设计模式介绍》第三章 工厂模式
《PHP设计模式介绍》第四章 单件模式
《PHP设计模式介绍》第五章 注册模式
《PHP设计模式介绍》第六章 伪对象模式
《PHP设计模式介绍》第七章 策略模式
《PHP设计模式介绍》第八章 迭代器模式
《PHP设计模式介绍》第九章 观测模式
《PHP设计模式介绍》第十章 规范模式
《PHP设计模式介绍》第十一章 代理模式
《PHP设计模式介绍》第十二章 装饰器模式

PHP中使用ASP.NET AJAX


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 198 ::
收藏到网摘: n/a

 借助于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", "[email protected]", 1000);    }}

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

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

大功告成!

 编写调用页面

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

<head>    <title>ASP.NET AJAX On PHP Demotitle><script type="text/javascript" src="MicrosoftAjaxLibrary/MicrosoftAjax.js"><script>    "text/javascript" src="EmployeeService.php/js">script>head>

程序的UI部分很简单,按钮用来触发异步调用,

用来显示调用结果: 

 <body>    <input id="btnGetEmployee" type="button" value="Get an Employee" onclick="return btnGetEmployee_onclick()" />    <div id="resultDiv">    div>body>

在该按钮的click事件处理函数中,调用该Service,语法也和ASP.NET AJAX中一致,非常方便:

function btnGetEmployee_onclick(){    EmployeeService.GetEmployee(onSucceeded);}

在回调函数中,把得到的Employee对象显示到resultDiv中:

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();}

大功告成!

 示例程序界面

第一次访问

点击Get an Employee按钮后