本文實例講述了ajax基本通用代碼。分享給大家供大家參考,具體如下:
<html>
<head>
<script type="text/JavaScript">
var xmlhttp
function loadXMLDoc(url)
{
// code for Mozilla, etc.
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest()
xmlhttp.onreadystatechange=state_Change
xmlhttp.open("GET",url,true)
xmlhttp.send(null)
}
// code for IE
else if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
if (xmlhttp)
{
xmlhttp.onreadystatechange=state_Change
xmlhttp.open("GET",url,true)
xmlhttp.send()
}
}
}
function state_Change()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
{
// if "OK"
if (xmlhttp.status==200)
{
document.getElementById('T1').innerHTML=xmlhttp.responseText
}
else
{
alert("Problem retrieving data:" + xmlhttp.statusText)
}
}
}
</script>
</head>
<body onload="loadXMLDoc('test_xmlhttp.txt')">
<div id="T1" style="border:1px solid black;height:40;width:300"></div><br />
<button onclick="loadXMLDoc('test_xmlhttp2.txt')">Click</button>
</body>
</html>
更多關於ajax相關內容感興趣的讀者可查看本站專題:《jquery中Ajax用法總結》、《JavaScript中ajax操作技巧總結》、《PHP+ajax技巧與應用小結》及《asp.net ajax技巧總結專題》。
希望本文所述對大家ajax程序設計有所幫助。