博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个简单的ajax
阅读量:6278 次
发布时间:2019-06-22

本文共 3674 字,大约阅读时间需要 12 分钟。

function_test.js文件
/*
*
 * @author Sam Lin
 * @date:February 14 ,2007
 * @contact: linsentong@126.com
 
*/
function
 $()
{
    
var
 elements
=
 
new
 Array();
   
    
for
(
var
 i 
=
0
;i 
<
 arguments.length;i
++
)
    {
      
//
取得参数的每一个值
       
var
 element
=
 arguments[i];
       
if
(typeofelement 
==
 
'
string
'
)
          element 
=
document.getElementById(element);
      
      
//
如果只是一个参数则返回
      
if
(arguments.length 
==
 
1
)
          returnelement;
      
      elements.push(element);
    }
    returnelements;
}
//
初始化XMLHttpRequest
var
 xmlHttp;
function
 InitiateXMLHttpRequest()
{
   
if
(window.XMLHttpRequest)
    {
       xmlHttp 
=
new
 window.XMLHttpRequest();
    }
    
else
    {
       
try
       {
          xmlHttp 
=
new
 ActiveXObject(
"
Msxml2.XMLHTTP
"
);
       }
      
catch
(e){
          
try
{
             xmlHttp 
=
new
 ActiveXObject(
"
Microsoft.XMLHTTP
"
);
          }
         
catch
(e)
          {
             xmlHttp 
=
false
;
          }
       }
    }
    returnxmlHttp;
}
function
 CallServer(url)
{
   
if
(xmlHttp)
    {
      xmlHttp.open(
"
GET
"
,url,
true
);
      xmlHttp.onreadystatechange 
=
 DoCallBack;
      xmlHttp.send(
null
);
    }
}
function
 DoCallBack()
{
    
var
 msg 
=
$(
"
divMessage
"
).innerHTML;
   
if
(xmlHttp.readyState 
==
 
4
)
    {
       msg 
+=
"
Callback the server successful!<br/>ResponseText:
"
+
xmlHttp.responseText
+
"
<br />
"
;
    }
    
else
    {
       msg 
+=
"
State:
"
+
xmlHttp.readyState 
+
"
<br />
"
;
    }
}
<%
@ Page Language
=
"
C#
"
 ContentType
=
"
text/html
"
ResponseEncoding
=
"
gb2312
"
 
%>
<!
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
>
<
meta 
http-equiv
="Content-Type"
 content
="text/html;charset=gb2312"
 
/>
<
script 
type
="text/javascript"
src
="function_test.js"
></
script
>
       
<
scripttype
="text/javascript"
>
          functiontest()
          {
             
var
 objTest
=
 $(
"
txtName
"
);
            alert(objTest.value);
          }
          
var
 xmlHttp
=
 InitiateXMLHttpRequest();
         alert(xmlHttp);
          functionCallServer(url)
          {
            
if
(xmlHttp)
             {
               
if
($(
"
txtName
"
).value 
==
 
""
)
                {
                  alert(
"
用户名不用为空!
"
);
                  
return
;
                }
                
else
                {
                   url 
+=
$(
"
txtName
"
).value;
                }
               xmlHttp.open(
"
POST
"
,url,
true
);
               xmlHttp.onreadystatechange 
=
 DoCallBack;
               xmlHttp.send(
null
);
             }
          }
       functionDoCallBack()
       {
         
          
var
 msg 
=
$(
"
divMessage
"
).innerHTML;
          
//
msg ="HH";
          
var
 tmp 
=
""
;
         
if
(xmlHttp.readyState 
==
 
4
)
          {
            document.getElementById(
"
results
"
).innerHTML 
=
xmlHttp.responseText;
             msg 
+=
"
Callback the server successful!<br/>ResponseText:
"
+
xmlHttp.responseText
+
"
<br />
"
;
             tmp 
+=
"
Callback the server successful!<br/>ResponseText:
"
+
xmlHttp.responseText
+
"
<br />
"
;
          }
          
else
          {
             msg 
+=
"
State:
"
+
xmlHttp.readyState 
+
"
<br />
"
;
             tmp
+=
"
State:
"
+
xmlHttp.readyState 
+
"
<br />
"
;
            
          }
         alert(msg);
       }
       
//
var sTemp= document.getElementById("spanMessage");
      
//
alert(sTemp.innerHTML);
      
       functionTest()
       {
         alert($(
"
lblMessage
"
).innerHTML);
       }
      
</
script
>
<
title
>
无标题文档
</
title
>
</
head
>
<
body 
MS_POSITIONING
="GridLayout"
>
       
<
formid
="Form1"
 method
="post"
 runat
="server"
>
          
<
inputtype
="text"
 id
="txtName"
 name
="txtName"
>
 
<
input 
type
="button"
id
="btnOk"
 name
="btnOk"
 value
="OK"
onclick
="javascript:test()"
>
          
<
br
/>
          
<
inputtype
="button"
 value
="CallBack"
 id
="btnCallBack"
onclick
="CallServer('http://localhost/ch33/sendCallBack.aspx?name=')"
 
/>
          
<
divid
="divMessage"
></
div
>
          
<
spanid
="spanMessage"
>
t
</
span
>
         
<
asp:Label 
ID
="lblMessage"
runat
="server"
>
OOO--
</
asp:Label
>
          
<
inputtype
="button"
 id
="btnTest"
 value
="Test"
 onclick
="Test()"
/>
      
</
form
>
       
<
div 
id
="results"
></
div
>
</
body
>
</
html
>
ajax.aspx.cs文件
Page_Load事件如下:
private
 
void
 Page_Load(
object
 sender, System.EventArgs e)
       {
          
//
在此处放置用户代码以初始化页面
         
if
(Request.QueryString[
"
name
"
==
 
null
)
          {
            Response.Write(
"
Sorry the programe is wrong!
"
);
          }
          
else
          {
             
string
 name
=
 Request.QueryString[
"
name
"
].ToString();
            Response.Write(
"
Welcome to use callback :
"
+
name);
         
            
          }

转载地址:http://rhyva.baihongyu.com/

你可能感兴趣的文章
实操分享:看看小白我如何第一次搭建阿里云windows服务器(Tomcat+Mysql)
查看>>
Sphinx 配置文件说明
查看>>
数据结构实践——顺序表应用
查看>>
python2.7 之centos7 安装 pip, Scrapy
查看>>
机智云开源框架初始化顺序
查看>>
Spark修炼之道(进阶篇)——Spark入门到精通:第五节 Spark编程模型(二)
查看>>
一线架构师实践指南:云时代下双活零切换的七大关键点
查看>>
ART世界探险(19) - 优化编译器的编译流程
查看>>
玩转Edas应用部署
查看>>
music-音符与常用记号
查看>>
sql操作命令
查看>>
zip 数据压缩
查看>>
Python爬虫学习系列教程
查看>>
【数据库优化专题】MySQL视图优化(二)
查看>>
【转载】每个程序员都应该学习使用Python或Ruby
查看>>
PHP高级编程之守护进程,实现优雅重启
查看>>
PHP字符编码转换类3
查看>>
rsync同步服务配置手记
查看>>
http缓存知识
查看>>
Go 时间交并集小工具
查看>>