[Serializable()] publicclass User { privateint _userId; privatestring _firstName; privatestring _lastName; publicint userId { get{ return _userId; } } publicstring FirstName { get{ return _firstName; } } publicstring LastName { get{ return _lastName; } } public User(int _userId, string _firstName, string _lastName) { this._userId = _userId; this._firstName = _firstName; this._lastName = _lastName; } public User(){} [Ajax.AjaxMethod()] publicstatic User GetUser(int userId) { //Replace this with a DB hit or something :) returnnew User(userId,"Michael", "Schwarz"); } }
五:使用JS,调用服务器端函数 在前面这些准备工作都做好后,即可以在JS里书写调用的函数了。具体的调用方法有两种(拿ServerSideAdd举例):
1.我们可以直接调用如<name of class>.<name of server side function>,这里就是noFlash.ServerSideAdd(100,99)。
2.添加一个处理返回值的函数,这里就是noFlash.ServerSideAdd(100,99,ServerSideAdd_CallBack)。其中,ServerSideAdd_CallBack就是用来处理服务器端运行ServerSideAdd后返回的值的函数。具体的代码如:
<script language="javascript"> noFlash.ServerSideAdd(100,99,ServerSideAdd_CallBack); function ServerSideAdd_CallBack(response){ if (response.error !=null){ alert(response.error); return; } alert(response.value); } </script>