现在Ajax是越来越流行了,它的无刷新效果太吸引人了,认识下。
var xmlHttpReq = new ActiveXObject("MSXML2.XMLHTTP.3.0"); xmlHttpReq.open("GET", "http://localhost/books.xml", false); xmlHttpReq.send(); alert(xmlHttpReq.responseText);在非IE的浏览器中,需要用new XMLHttpRequest()来创建对象,如下:
var xmlHttpReq = new XMLHttpRequest(); xmlHttpReq.open("GET", "http://localhost/books.xml", false); xmlHttpReq.send(); alert(xmlHttpReq.responseText);
XmlHttp的属性:
onreadystatechange* 指定当readyState属性改变时的事件处理句柄。只写
readyState 返回当前请求的状态,只读.
responseBody 将回应信息正文以unsigned byte数组形式返回.只读
responseStream 以Ado Stream对象的形式返回响应信息。只读
responseText 将响应信息作为字符串返回.只读
responseXML 将响应信息格式化为Xml Document对象并返回,只读
status 返回当前请求的http状态码.只读
statusText 返回当前请求的响应行状态,只读
XmlHttp的方法:
abort 取消当前请求
getAllResponseHeaders 获取响应的所有http头
getResponseHeader 从响应信息中获取指定的http头
open 创建一个新的http请求,并指定此请求的方法、URL以及验证信息(用户名/密码)
send 发送请求到http服务器并接收回应
setRequestHeader 单独指定请求的某个http头
XmlHttp带来了很多方便
例如可以用它来查询用户名:
function CheckUserIsExists()
{
var Http = new ActiveXObject("Microsoft.XMLHTTP"); //建立XMLHTTP对象
Http.open("POST","manage.asp?name="+document.form1.username.value,"false");
Http.send();if(Http.responseText=="N"){alert("恭喜,您可以使用此用户名!");return true;}else{alert("用户已存在!");}return false;
}