源生Ajax
ajax的用途:用来实现网页的一些效果,比如(局部刷新,表单验证,交互式网页等)。
实现ajax主要靠XMLHttpRequest类。
*主意:这里XMLHttpRequest不能兼容IE6版本浏览器以及以下的产品,需要用ObjectXActive来实现。
XMLHttpRequest的主要方法:
1、open(method,url,async)
2、send(String)
3、responseXML
4、responseText
实现步骤:
1、实例化 XMLHttpRequest
2、调用open()与服务器建立连接
3、创建监听
4、发送请求(传递参数)
<script>
//声明一个变量
var xmlrequest;
//创建XMLHttpRequst对像
xmlrequest = new XMLHttpRequest();
console.log(xmlrequest);
//发送请求
xmlrequest.open("GET", "student.xml", true);//与服务器建立连接
xmlrequest.send(null);//发送请求
//回调函数
xmlrequest.onreadystatechange=function(){//建立监听
if(xmlrequest.readyState == 4){//XMLHttpRequst对象读取相应结束
if(xmlrequest.status == 200){//HTTP的状态吗 服务器响应正常
//获取响应的结束
var data = xmlrequest.responseText;
}
}
}
</script>
注*一般在请求方式是GET时,参数是拼接在请求地址中的,所以send()方法可写null;但是在请求方式是post时,参数就一定需要写在send()方法中;