国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

用.net 處理xmlHttp發送異步請求

2019-11-18 16:46:26
字體:
來源:轉載
供稿:網友

最近正在拜讀<<Ajax in Action>>這本書,運用書中知識,結合.net,寫了這篇用.net 處理xmlHttp發送異步請求的文章。

我們要達到的目的是點擊按鈕,獲得服務器的當前時間,aspx的html如下:
Html
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Linkedu.Web.WebWWW.Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "

<html xmlns="<head runat="server">
    <title>測試</title>
    <scr
ipt language="javascript" src="Javascript/   <script language="javascript" src="javascript/xmlhttp.js"></script>
   <script language="javascript" src="javascript/eventRouter.js"></script>
    <script language="javascript" src="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>

要用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, "<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*/

 

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(@"<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();
                }
            }
        }

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

http://m.survivalescaperooms.com/Files/laiwen/XmlHttpNet.rar
http://m.survivalescaperooms.com/laiwen/archive/2006/12/26/604050.html


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 三亚市| 石台县| 固始县| 肇庆市| 麻栗坡县| 通化县| 浦县| 石嘴山市| 信丰县| 洪江市| 彭州市| 乐山市| 成都市| 泸水县| 宜昌市| 搜索| 祁门县| 德令哈市| 江阴市| 田阳县| 什邡市| 青海省| 织金县| 诸暨市| 正安县| 富阳市| 漾濞| 武强县| 广西| 益阳市| 阳谷县| 丰台区| 灌南县| 磴口县| 尚义县| 江山市| 黄陵县| 当阳市| 安龙县| 韩城市| 邹城市|