本文摘自 http://www.codingsky.com/blog?blogid=404
习惯了用框架进行ajax访问?可别忘了ajax访问的基本代码与过程,本文让我们体验一下,不使用框架的最简单的进行ajax请求的方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 |
var
xmlHttpRequest; //XMLHttpRequest对象名 function
sendRequest() { //发送http请求 getXMLHttpRequest(); //获得一个XMLHttpRequest对象到xmlHttpRequest xmlHttpRequest.onreadystatechange = stateChange; //http状态变化时执行的操作 xmlHttpRequest.open( "GET" , "simpleAjax.xml" ); //连接 xmlHttpRequest.send( null ); //向服务器发送请求(这里内容为空) } function
getXMLHttpRequest() { if (window.ActiveXObject) { //IE类 xmlHttpRequest = new
ActiveXObject( "Microsoft.XMLHTTP" ); } else
if (window.XMLHttpRequest) { //Chrome类 xmlHttpRequest = new
XMLHttpRequest(); } } function
stateChange() { if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) { //请求已完成且状态OK alert(xmlHttpRequest.responseText); //作出的反应 } } |
原文:http://www.cnblogs.com/blogjourney/p/3533098.html