在PHP中使用ASP.NET AJAX

news/2024/7/10 22:01:44 标签: ASP.net, .net, ASP, PHP, Ajax
<iframe align="top" marginwidth="0" marginheight="0" src="http://www.zealware.com/46860.html" frameborder="0" width="468" scrolling="no" height="60"></iframe>

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

下载安装

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

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

下载地址:http://www.codeplex.com/phpmsajax/Release/ProjectReleases.aspx?ReleaseId=1692

安装方法:

  1. 下载PHP for Microsoft AJAX Library并解压缩
  2. 下载Microsoft AJAX Library(http://ajax.asp.net
  3. PHP Web Service代码中include一下MSAjaxService.php。
  4. 在调用该Web Service的页面中,引入MicrosoftAjax.js文件。

下面来看一个“经典”的场景:调用服务器端方法取得复杂类型。

编写Service文件

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

require_once 'MSAjaxService.php';
<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>

然后定义一个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平台上的语法一样:

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

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

用来显示调用结果:

body>
    input id="btnGetEmployee" type="button" 
        value="Get an Employee" onclick="return btnGetEmployee_onclick()" />
    div id="resultDiv">
    <!--</span-->div>
<!--</span-->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();
}

大功告成!

示例程序界面

第一次访问

PHPASP.NETAJAX_B8/image%7B0%7D%5B1%5D.png" alt="" />

点击Get an Employee按钮后

PHPASP.NETAJAX_B8/image%7B0%7D%5B3%5D.png" alt="" />

示例程序代码下载

在这里呢:ASPNETAJAXOnPHP.zip

好一阵子没用PHP了,生疏了不少。不过照猫画虎还是做出来了,这个项目我觉得非常有前途,各位朋友不妨参与一下。这一段比较忙,没写什么技术文章,这一篇也是草草而为,请朋友们见谅。



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1569920



http://www.niftyadmin.cn/n/598949.html

相关文章

《ASP.NET AJAX程序设计 第I卷》的心路历程

<iframe align"top" marginwidth"0" marginheight"0" src"http://www.zealware.com/46860.html" frameborder"0" width"468" scrolling"no" height"60"></iframe>06年3月份&…

CSDN的Blog要如何配置才能够用Windows Live Writer发布带有图片的Post?

<iframe align"top" marginwidth"0" marginheight"0" src"http://www.zealware.com/46860.html" frameborder"0" width"468" scrolling"no" height"60"></iframe>如题&#xff0…

Quartz.net官方开发指南 第六课 : CronTrigger

如果你需要像日历那样按日程来触发任务&#xff0c;而不是像SimpleTrigger 那样每隔特定的间隔时间触发&#xff0c;CronTriggers通常比SimpleTrigger更有用。 使用CronTrigger&#xff0c;你可以指定诸如“每个周五中午”&#xff0c;或者“每个工作日的9:30”或者“从每个周一…

less入门初体验

css有众多的预处理器&#xff0c;其中比较流行的三个是less、sass、stylus&#xff0c;本文就一起来体验less编写css代码官方文档:http://lesscss.org/官网栗子: base: #f938ab;.box-shadow(style, c) when (iscolor(c)) {-webkit-box-shadow: style c;box-shadow: sty…

递归算法在javascript中使用的小技巧 (javascript的对象封装方法介绍)

<iframe align"top" marginwidth"0" marginheight"0" src"http://www.zealware.com/46860.html" frameborder"0" width"468" scrolling"no" height"60"></iframe>By Ben比如求 1…

利用css里expression来实现界面对象的批量控制

<iframe align"top" marginwidth"0" marginheight"0" src"http://www.zealware.com/46860.html" frameborder"0" width"468" scrolling"no" height"60"></iframe>By Ben利用css里…

早安 2017年!

2016年就那么过去啦&#xff01;2017年我们来啦&#xff01;Hello 2017年&#xff01; 2016年做了很多事情&#xff0c;学了很多东西&#xff0c;自己思考了很多&#xff0c;也成长了很多&#xff01; 就像莎士比亚说的&#xff1a;“一切过往&#xff0c;皆成为序章。” 2016年…

Quartz.net官方开发指南 第七课 : TriggerListeners和JobListeners

监听器是在scheduler事件发生时能够执行动作的对象。可以看出&#xff0c;TriggerListeners接收与triggers相关的事件&#xff0c;而JobListeners则接收与Job相关的事件。Trigger相关的事件包括&#xff1a;trigger触发、trigger未触发&#xff0c;以及trigger完成&#xff08;…