扫描二维码关注

首页 APP开发小程序开发 微信公众号 网站建设 营销推广 经典案列 产品服务 关于我们

“学习不仅是掌握知识”

向书本学习,还要向实践学习、向生活学习。消化已有知识,
而且要力求有所发现、有所发明、有所创造

用.net 处理xmlHttp发送异步请求

2019/3/14 8:48:36

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


深圳市南山区南山街道南海大道西桂庙路北阳光华艺大厦1栋4F、4G-04

咨询电话:136 8237 6272
大客户咨询:139 0290 5075
业务QQ:2062128898
业务QQ:195006118
技术QQ:179981967

精锐软件

Copyright© 2018-2022 深圳精锐软件技术有限公司 All Rights Reserved. ICP备案号:粤ICP备18108116号-1 公安备案号:粤公网安备44030502003401号