风在芯中
金牌会员
 
kuliu.com
UID 25808
精华
0
积分 2548
帖子 716
金钱 2548 喜悦币
威望 0
人脉 0
阅读权限 70
注册 2004-1-19 来自 福建 福州
状态 在线
|
[推荐阅读] xajax返回404时我怎么处理?
从google拿下的一段很简单的ajax代码
可以学习一下。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>myajax</title>
</head>
<body>
<script>
//-----------------------------------
//check showbar name
function getState(xmlhttp) {
//alert(xmlhttp.readyState+','+xmlhttp.status);
if (xmlhttp.readyState != 4) return false;
if (xmlhttp.status != 200) return false;
alert(xmlhttp.responseText);
}
//-----------------------------------
// Browser detection
var agt = navigator.userAgent.toLowerCase();
var is_opera = (agt.indexOf("opera") != -1);
var is_ie = (agt.indexOf("msie") != -1) && document.all && !is_opera;
var is_ie5 = (agt.indexOf("msie 5") != -1) && document.all;
// ----------------------------------
// XML Http requests
function CreateXmlHttpReq(handler) {
var xmlhttp = null;
if (is_ie) {
// Guaranteed to be ie5 or ie6
var control = (is_ie5) ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
try {
xmlhttp = new ActiveXObject(control);
} catch(e) {
// TODO: better help message
alert("You need to enable active scripting and activeX controls");
DumpException(e);
}
} else {
// Mozilla
xmlhttp = new XMLHttpRequest();
}
xmlhttp.onreadystatechange = function() {
handler(xmlhttp);
}
return xmlhttp;
}
var uniqnum_counter = (new Date).getTime();
function UniqueNum() {
++uniqnum_counter;
return uniqnum_counter;
}
// Send a (hidden, non blocking) update message to the server
// by sending a xmlhttp request
function SendServerRequest(url) {
StartGETRequest(url, function() {});
}
//
function CheckRequest(url) {
StartGETRequest(url, getState);
}
// Send a request to the server
function StartGETRequest(url, handler) {
var xmlhttp = CreateXmlHttpReq(handler);
xmlhttp.open('GET', url + "&rand=" + UniqueNum(), true);
xmlhttp.send(null);
}
// Send a post request to the server
function StartPOSTRequest(url, data, handler) {
var xmlhttp = CreateXmlHttpReq(handler);
xmlhttp.open('POST', url, true);
xmlhttp.send(data);
}
//-->
</script>
<script>
function post(state){
//if (state == 1) {
// SendServerRequest("http://127.0.0.1/myajax/passive.php?state=off");
//} else {
// SendServerRequest("http://127.0.0.1/myajax/passive.php?state=on");
// }
if(!CheckRequest("http://192.168.1.210/myajax/passive.php?state=on"))
return false;
}
</script>
<a onclick="return post(0);">click here</a>
</body>
</html>
|  酷溜网 |
|