All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
图片保存到数据库和从数据库读取图片并显示_[Asp.Net教程]
以图换字的几个方法及思路_[Html教程]
用ASP.NET,2.0在Oracle中存取图片(文件)的操作_.net资料_编程技术
组合.NET数据控件构建强大用户接口_.net资料_编程技术
left join的总结_[SQL Server教程]
深入理解SQL Server 中的错误处理_[SQL Server教程]
IsPostBack深入分析_[Asp.Net教程]
phpMyAdmin安装配置方法全过程_php资料_编程技术
总结SQL Server与Access语法小差异_[SQL Server教程]
Response输出可以加批注的Excel_[Asp.Net教程]
Reading number is top 10 pictures
30 beautiful school beauty3
如果没有好报,为什么要做好人?
The money of more than 100 countries and regions20
Hunan province aizhai super-large suspension bridge open to traffic and 4 world first2
Exquisite decoration is not paying too much3
采访美女孙菲菲
Born After 90 Beijing sports university campus flower photos4
9.3阅兵全景图4-陸海空现代化兵种方阵梯队
西游四格漫画(一)
男人帮杂志里的惹火性感美女1
Download software ranking
Tram sex maniac 2 (H) rar bag6
VC++6.0培训教程
Red cliff
实战黑客不求人
双旗镇刀客A
Boxer's Top ten classic battle6
圣殿祭司的ASP.NET.2.0.开发详解-使用C#
Boxer's Top ten classic battle1
JSP+Ajax Web development typical examples
Proficient in JavaScript
aaa published in(发表于) 2013/12/15 8:53:57 Edit(编辑)
用.net,处理xmlHttp发送异步请求_.net资料_编程技术

用.net,处理xmlHttp发送异步请求_.net资料_编程技术

用.net 处理xmlHttp发送异步请求_.net资料_编程技术-你的首页-uuhomepage.com







最近正在拜读<>这本书,运用书中知识,结合.net,写了这篇用.net 处理xmlHttp发送异步请求的文章。




我们要达到的目的是点击按钮,获得服务器的当前时间,aspx的html如下:
Html
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Linkedu.Web.WebWWW.Default" %>




http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">




http://www.w3.org/1999/xhtml" >

测试








用Post方式获得服务器的当前时间

用Get方式获得服务器的当前时间











要用javascript 发送xmlHttp 请求必须解决的问题是跨浏览器的支持。我们把xmlHttp的发送封装在一个javascript对象中,同时在这个对象中解决了跨浏览器支持的问题。代码如下:




xmlHttp对象
/**//*
url-loading object and a request queue built on top of it
*/




/**//* namespacing object */
var net=new Object();




net.READY_STATE_UNINITIALIZED=0;
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);
}




net.xmlHttp.prototype.loadXMLDoc=function(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);
}
}
}




net.xmlHttp.prototype.defaultError=function(){
alert("error fetching data!"
+"\n\nreadyState:"+this.req.readyState
+"\nstatus: "+this.req.status
+"\nheaders: "+this.req.getAllResponseHeaders());
}







下面开始写发送xmlHttp请求的代码:




default.js
//全局xmlHttp对象
var cobj;




/**//* Post begin*/
//绑定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, "", "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*/







function dealResult()
{
var dobj = document.getElementById("divResult");
dobj.innerHTML = cobj.req.responseXML.text;
}





window.onload = function()
{
//绑定Post发送xmlHttp事件到btnTestPost
loadTestPost();
//绑定Get发送xmlHttp事件到btnTestGet
loadTestGet();
};




最后是.net处理xmlHttp的代码
.net 处理xmlHttp请求
public class DefaultHandler : IHttpHandler
{
protected XmlDocument _xmlResult;




public void ProcessRequest(HttpContext context)
{
if (context.Request["T"] != null)
{//GET xmlhttp测试
context.Response.ContentType = "text/xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(string.Format(@"", 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(@"", System.DateTime.Now));
xmlDoc.Save(context.Response.OutputStream);
context.Response.End();
}
}
}




public bool IsReusable
{
get
{
return false;
}
}
}






















添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.