掃描二維碼關注

首頁 APP開(kāi)發小(xiǎo)程序開(kāi)發 微信公衆号 網站建設 營銷推廣 經典案列 産品服務 關于我們

“學習(xí)不僅是掌握知識”

向書(shū)本學習(xí),還要向實踐學習(xí)、向生活學習(xí)。消化已有(yǒu)知識,
而且要力求有(yǒu)所發現、有(yǒu)所發明(míng)、有(yǒu)所創造

用(yòng).net 處理(lǐ)xmlHttp發送異步請求

2019/3/14 8:48:36

用(yòng).net 處理(lǐ)xmlHttp發送異步請求

們要達到(dào)的(de)目的(de)是點擊按鈕,獲得服務器(qì)的(de)當前時間,aspx的(de)html如(rú)下:
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>測試</title>
    <script language="javascript" src="javascript/prototype/extras-array.js"></script>
   <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">
        用(yòng)Post方式獲得服務器(qì)的(de)當前時間
        <input id="btnTestPost" type="button" value="Post" />
        用(yòng)Get方式獲得服務器(qì)的(de)當前時間
        <input id="btnTestGet" type="button" value="Get" />
        <div id="divResult"></div>
</form>
</body>
</html>

要用(yòng)javascript 發送xmlHttp 請求必須解決的(de)問題是跨浏覽器(qì)的(de)支持。我們把xmlHttp的(de)發送封裝在一個(gè)javascript對象中,同時在這個(gè)對象中解決了跨浏覽器(qì)支持的(de)問題。代碼如(rú)下:

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);
    &nb


深圳市南山區南山街(jiē)道南海(hǎi)大(dà)道西(xī)桂廟路(lù)北陽光(guāng)華藝大(dà)廈1棟4F、4G-04

咨詢電話(huà):136 8237 6272
大(dà)客戶咨詢:139 0290 5075
業(yè)務QQ:195006118
技術(shù)QQ:179981967

更多可(kě)以了解的(de)信息

客戶案列
新聞資訊
資質榮譽
團隊風采
項目進度查詢

售前QQ咨詢
QQ溝通 項目QQ溝通

精銳軟件(jiàn)

Copyright© 2018-2023 深圳市無窮大軟件技術有限公司 All Rights Reserved. 京ICP證000000号 公安備案号:粵公網安備44030502009460号