XMLHttpRequest对象的属性:
1.readyState: 只读属性,目前状态的长整数,值4表示请求处理完成
2.responseBody: 只读属性,取得伺服器的回应,传回非符号位元组的阵列
3.responseSteam: 只读属性,取得伺服器的回应,传回IStream资料流物件
4.responseText: 只读属性,取得伺服器的回应,传回字串
5.responseXML: 只读属性,取得伺服器的回应,传回XML DOM物件
6.status: 只读属性,取得HTTP状态码,例如:200,302等
7.statusText: 只读属性,取得HTTP状态信息字串,例如OK
8.onreadystatechange: 只读属性,执行readyState属性改变的事件处理,设定处理的程序
----------------------------------------
readyState属性的状态值有:
0 尚未初始化,即尚未呼叫open()方法
1 目前正在载入中,即HTTP请求已经准备好,但是尚未呼叫send()方法,但已调用open()方法
2 载入完成,并且已经将HTTP请求送出,伺服器正在处理,发送数据调用send()方法以后
3 伺服器正在处理HTTP请求中
4 已经成功回应HTTP请求,客户端可以执行进一步处理
----------------------------------------
XMLHttpRequest对象的方法:
1.about(): 取消目前的HTTP请求
2.getAllResponseHeaders(): 取得全部HTTP标头的内容
3.getResponseHeader(HeaderName): 取得全部HTTP标头名称的内容
4.open(method,url,async,userid,pwd):开启HTTP请求:method:"GET"||"POST",async:true || false 例如:xmlhttp.open("GET",url,false);
5.send(): 传送HTTP请求到伺服器
6.setRequestHeader(HeaderName,value):使用者自订的HTTP标头资料
========================
一个示例:
1. default.aspx
<head runat="server">
<title>javascript测试</title>
<script language="javascript" type="text/javascript">
var httpRequest;
//创建XMLHttpRequest对象
function createXMLHttpRequest() {
if (window.XMLHttpRequest) {
//IE7,mozilla,safari
httpRequest = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
//ie5,ie6,找出最新版的MSXML剖析器
var msxmls = ["MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp", "Microsoft.XMLHttp"];
for (var i = 0; i < msxmls.length; i++) {
try {
httpRequest = new ActiveXObject(msxmls[i]);
break;
}
catch (e) {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
}
}
return httpRequest;
}
var currentobj;
function showTime(obj) {
currentobj = obj;
document.getElementById(obj).innerHTML = "正在读取时间......";
sendRequest("Search.aspx?code=" + Math.random());
}
function sendRequest(url) {
createXMLHttpRequest();
httpRequest.onreadystatechange = processRequest;
httpRequest.open("GET", url, false);
httpRequest.send();
}
function processRequest() {
if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {
document.getElementById(currentobj).innerHTML = httpRequest.responseText;
// alert(httpRequest.responseText);
}
else {
alert("取当前的时间失败!");
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button1" type="button" value="button" onclick="showTime('span1')" />
<span id="span1"></span>
</div>
</form>
</body>
</html>
2. Search.aspx文件中只保留最上面一句:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Search.aspx.cs" Inherits="wawaEshop.WebSite.Search" %>
Search.aspx.cs文件:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(DateTime.Now.ToString());
}