最近正在拜读<<Ajax in Action>>这本书,运用书中知识,结合.net,写了这篇用.net 处理xmlHttp发送异步请求的文章。 我们要达到的目的是点击按钮,获得服务器的当前时间,aspx的html如下: <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“> <html xmlns=”http://www.w3.org/1999/xhtml” > 要用javascript 发送xmlHttp 请求必须解决的问题是跨浏览器的支持。我们把xmlHttp的发送封装在一个javascript对象中,同时在这个对象中解决了跨浏览器支持的问题。代码如下: xmlHttp对象 /**//* namespacing object */ net.READY_STATE_UNINITIALIZED=0; net.xmlHttp.prototype.loadXMLDoc=function(url, params, method, contentType){ net.xmlHttp.prototype.defaultError=function(){ 下面开始写发送xmlHttp请求的代码: default.js /**//* Post begin*/ function dealResult() 最后是.net处理xmlHttp的代码 public void ProcessRequest(HttpContext context) public bool IsReusable http://www.cnblogs.com/Files/laiwen/XmlHttpNet.rar
Html
<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”Default.aspx.cs” Inherits=”Linkedu.Web.WebWWW.Default” %>
<head runat=”server”>
<title>测试</title>
<script language=”javascript” src=”http://www.knowsky.com/javascript/prototype/extras-array.js”></script>
<script language=”javascript” src=”http://www.knowsky.com/javascript/xmlHttp.js”></script>
<script language=”javascript” src=”http://www.knowsky.com/javascript/eventRouter.js”></script>
<script language=”javascript” src=”http://www.knowsky.com/Default.js”></script>
<script language=”javascript”>
</script>
</head>
<body>
<form id=”form1″ runat=”server”>
用Post方式获得服务器的当前时间
<input id=”btnTestPost” type=”button” value=”Post” />
用Get方式获得服务器的当前时间
<input id=”btnTestGet” type=”button” value=”Get” />
<div id=”divResult”></div>
</form>
</body>
</html>
/**//*
url-loading object and a request queue built on top of it
*/
var net=new Object();
net.READY_STATE_LOADING=1;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIVE=3;
net.READY_STATE_COMPLETE=4;
/**//*— content loader object for cross-browser requests —*/
net.xmlHttp=function(url, onload, params, method, contentType, onerror){
this.req=null;
this.onload=onload;
this.onerror=(onerror) ? onerror : this.defaultError;
if(typeof(method) == “undefined” || method == null)
{
method = “POST”;
}
this.loadXMLDoc(url, params, method, contentType);
}
if (!method){
method=”GET”;
}
if (!contentType && method==”POST”){
contentType=application/x-www-form-urlencoded;
}
if (window.XmlHttpRequest){
this.req=new XmlHttpRequest();
} else if (window.ActiveXObject){
this.req=new ActiveXObject(“Microsoft.xmlHttp”);
}
if (this.req){
try{
var loader=this;
this.req.onreadystatechange=function(){
net.xmlHttp.onReadyState.call(loader);
}
this.req.open(method,url,true);
if (contentType){
this.req.setRequestHeader(Content-Type, contentType);
}
this.req.send(params);
}catch (err){
this.onerror.call(this);
}
}
}
net.xmlHttp.onReadyState=function(){
var req=this.req;
var ready=req.readyState;
if (ready==net.READY_STATE_COMPLETE){
var httpStatus=req.status;
if (httpStatus==200 || httpStatus==0){
this.onload.call(this);
}else{
this.onerror.call(this);
}
}
}
alert(“error fetching data!”
+”\n\nreadyState:”+this.req.readyState
+”\nstatus: “+this.req.status
+”\nheaders: “+this.req.getAllResponseHeaders());
}
//全局xmlHttp对象
var cobj;
//绑定Post发送xmlHttp事件到btnTestPost
function loadTestPost()
{
var iobj = document.getElementById(“btnTestPost”);
//btnTestPost按钮监听的绑定
var clickRouter=new jsEvent.EventRouter(iobj,”onclick”);
clickRouter.addListener(btnTestPostClick);
}
function btnTestPostClick()
{ // open参数 url, onload, params, method, contentType, onerror
cobj = new net.xmlHttp(“DefaultHandler.ashx”,dealResult, “<T/>”, “POST”);
}
/**//* Post end*/
/**//* Get begin*/
//绑定Get发送xmlHttp事件到btnTestGet
function loadTestGet()
{
var iobj = document.getElementById(“btnTestGet”);
//btnTestGet按钮监听的绑定
var clickRouter=new jsEvent.EventRouter(iobj,”onclick”);
clickRouter.addListener(btnTestGetClick);
}
function btnTestGetClick()
{ // open参数 url, onload, params, method, contentType, onerror
cobj = new net.xmlHttp(“DefaultHandler.ashx?T=1”,dealResult, null, “GET”);
}
/**//* Get end*/
{
var dobj = document.getElementById(“divResult”);
dobj.innerHTML = cobj.req.responseXML.text;
}
window.onload = function()
{
//绑定Post发送xmlHttp事件到btnTestPost
loadTestPost();
//绑定Get发送xmlHttp事件到btnTestGet
loadTestGet();
};
.net 处理xmlHttp请求
public class DefaultHandler : IHttpHandler
{
protected XmlDocument _xmlResult;
{
if (context.Request[“T”] != null)
{//GET xmlhttp测试
context.Response.ContentType = “text/xml”;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(string.Format(@”<time>GET:{0}</time>”, System.DateTime.Now));
xmlDoc.Save(context.Response.OutputStream);
context.Response.End();
}
else
{//POST xmlhttp测试
context.Response.ContentType = “text/xml”;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(context.Request.InputStream);
if (xmlDoc.DocumentElement.Name == “T”)
{
xmlDoc.LoadXml(string.Format(@”<time>POST:{0}</time>”, System.DateTime.Now));
xmlDoc.Save(context.Response.OutputStream);
context.Response.End();
}
}
}
{
get
{
return false;
}
}
}
http://www.cnblogs.com/laiwen/archive/2006/12/26/604050.html
用.net 处理xmlhttp发送异步请求_asp.net技巧
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 用.net 处理xmlhttp发送异步请求_asp.net技巧
相关推荐
-      对.net framework 反射的反思_asp.net技巧
-      .net3.5和vs2008中的asp.net ajax_asp.net技巧
-      使用asp.net ajax框架扩展html map控件_asp.net技巧
-      asp.net应用程序资源访问安全模型_asp.net技巧
-      photoshop初学者轻松绘制螺旋漩涡特效_photoshop教程
-      photoshop通道结合图层模式抠狗尾巴草_photoshop教程
-      web.config详解+asp.net优化_asp.net技巧
-      asp.net中多彩下拉框的实现_asp.net技巧