Ajax模拟用法【回调实现脚本实体类】
2008-05-20 16:56
225 查看
这里有几点。 我们用asp.net开发数据库应用时。时常要建立一些实体如
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public class UserEntity
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public string UserName
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
get...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
return "";}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public string UserEmail
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
get...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
return "" ;}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
下面要讲的是如何使用脚本来模拟这种实体
首先 要讲解概念 要返还类似这种实体脚本可以这么写
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
UserEntity=function()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
return ...{"UserName":"shenyi","UserEmail":"shenyi@csdn.net"}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
这样我们在 脚本中只要写
var scriptEntity=new UserEntity();
scriptEntity.UserName;
scriptEntity.UserEmail;
就可以引用到,好 脚本实体构建好了。接下来我们要从服务器取数据如下
新建一个aspx页面 假如叫 getData.aspx 在代码里写
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public void Page_load()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int userid=int.parse(Request.QueryString["uid"].ToString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string sql="select * from Users where userid="+userid.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
using(DataTable dt=SqlHelper.ExecuteDataSet(sql).Tables[0]) //SqlHelper 不要告诉我 不知道是什么
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 这里构建一个 字符 令 一个 脚本变量 等于 {"UserName":dt.rows[0]["username].......}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如 Response.Write("userReturnValue={.......//这里自己拼接}");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.End();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
以下是如何把脚本和这个 aspx页面建立联系 如下直接是代码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
var UserReturnValue="" //这个很重要一定要定义 因为在服务端要对其赋值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ComJS=function()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.GetEntitying=function(uid,callback)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var scriptObj=document.createElement("script");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scriptObj.type="text/javascript";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scriptObj.src="getData.aspx?uid="+uid
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
document.getElementsByTagName("head")[0].appendChild(scriptObj);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scriptObj.onreadystatechange=function()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(scriptObj.readyState=="loaded")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
callback.EndGetEntity(UserReturnValue);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
这里使用 callback回调 是最重要的一步 当脚本加载完毕后才会对UserReturnValue 进行赋值 因此我们不能直接获得这个实体.
上面脚本写完后 我们在页面要开始获得这个 实体
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
var cjs=new ComJS(); //初始化我们的脚本对象
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
function BeginGetEntity()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.EndGetEntity=function(entityDataList)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//entityDataList 这个就是最终的实体对象 你可以直接 用 entityDataList.UserName 或者 entityDataList.表里字段名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//来获取这个实体的对象
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 注意 业务操作 要在这个 函数内执行。因为这是一个回调。也就是当服务端把数据取到了 这边才开始执行
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
严格来说就是异步获取数据。类似Ajax 但是不使用 xmlhttp对象
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
var bge=new BeginGetEntity();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
cjs.GetEntity("3",bgelist); //这里的3代表我要取用户id等于3的 数据
好/ 整个用脚本构建实体的方法写好 希望对大家尤其是初学者 会有点用。其实在脚本中要实现ajax不光只有xmlhttp可以实现 很普通的脚本也能实现
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public class UserEntity
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public string UserName
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
get...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
return "";}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public string UserEmail
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
get...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
return "" ;}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
下面要讲的是如何使用脚本来模拟这种实体
首先 要讲解概念 要返还类似这种实体脚本可以这么写
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
UserEntity=function()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
return ...{"UserName":"shenyi","UserEmail":"shenyi@csdn.net"}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
这样我们在 脚本中只要写
var scriptEntity=new UserEntity();
scriptEntity.UserName;
scriptEntity.UserEmail;
就可以引用到,好 脚本实体构建好了。接下来我们要从服务器取数据如下
新建一个aspx页面 假如叫 getData.aspx 在代码里写
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public void Page_load()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int userid=int.parse(Request.QueryString["uid"].ToString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string sql="select * from Users where userid="+userid.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
using(DataTable dt=SqlHelper.ExecuteDataSet(sql).Tables[0]) //SqlHelper 不要告诉我 不知道是什么
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 这里构建一个 字符 令 一个 脚本变量 等于 {"UserName":dt.rows[0]["username].......}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如 Response.Write("userReturnValue={.......//这里自己拼接}");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.End();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
以下是如何把脚本和这个 aspx页面建立联系 如下直接是代码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
var UserReturnValue="" //这个很重要一定要定义 因为在服务端要对其赋值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ComJS=function()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.GetEntitying=function(uid,callback)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var scriptObj=document.createElement("script");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scriptObj.type="text/javascript";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scriptObj.src="getData.aspx?uid="+uid
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
document.getElementsByTagName("head")[0].appendChild(scriptObj);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scriptObj.onreadystatechange=function()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(scriptObj.readyState=="loaded")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
callback.EndGetEntity(UserReturnValue);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
这里使用 callback回调 是最重要的一步 当脚本加载完毕后才会对UserReturnValue 进行赋值 因此我们不能直接获得这个实体.
上面脚本写完后 我们在页面要开始获得这个 实体
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
var cjs=new ComJS(); //初始化我们的脚本对象
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
function BeginGetEntity()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.EndGetEntity=function(entityDataList)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//entityDataList 这个就是最终的实体对象 你可以直接 用 entityDataList.UserName 或者 entityDataList.表里字段名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//来获取这个实体的对象
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 注意 业务操作 要在这个 函数内执行。因为这是一个回调。也就是当服务端把数据取到了 这边才开始执行
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
严格来说就是异步获取数据。类似Ajax 但是不使用 xmlhttp对象
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
var bge=new BeginGetEntity();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
cjs.GetEntity("3",bgelist); //这里的3代表我要取用户id等于3的 数据
好/ 整个用脚本构建实体的方法写好 希望对大家尤其是初学者 会有点用。其实在脚本中要实现ajax不光只有xmlhttp可以实现 很普通的脚本也能实现
相关文章推荐
- 利用回调实现脚本实体类和模拟Ajax
- 利用回调实现脚本实体类和模拟Ajax
- 利用回调实现脚本实体类和模拟Ajax
- 模拟QQ侧滑控件 实现三种界面切换效果(知识点:回调机制,解析网络json数据,fragment用法等)。
- Ajax用法详细解析, 实现异步更新页面, 以及'post'和'get'请求属性介绍
- 轻松掌握Ajax.net系列教程四:用Ajax.net实现客户端回调(Callback)
- php实现模拟post请求用法实例
- ASP.NET回调实现AJAX
- JQuery的ajax的用法在asp中使用$.ajax()实现
- javascript模拟实现ajax加载框实例
- CentOS6.8 x86_64bit shell脚本实现模拟nginx服务启动与关闭
- [转]轻松掌握Ajax.net系列教程四:用Ajax.net实现客户端回调(Callback)
- 如何使用IFRAME来实现类似Ajax的技术----远程脚本
- 关于数据增量抽取的模拟实现——脚本实现
- javascript模拟实现ajax加载框实例
- JAVA EE 项目常用知识 之AJAX技术实现select下拉列表联动的两种用法(让你真正理解ajax)
- AJAX实现google输入自动完成的简单模拟
- 用AJAX实现google输入自动完成的简单模拟(转)
- 夺命雷公狗jquery---55---Ajax的高级实现,模拟发送get请求
- 夺命雷公狗jquery---56通过ajax高级实现模拟post请求