ajax基本通用代码示例
本文实例讲述了ajax基本通用代码。分享给大家供大家参考,具体如下:
<html>
<head>
<scripttype="text/JavaScript">
varxmlhttp
functionloadXMLDoc(url)
{
//codeforMozilla,etc.
if(window.XMLHttpRequest)
{
xmlhttp=newXMLHttpRequest()
xmlhttp.onreadystatechange=state_Change
xmlhttp.open("GET",url,true)
xmlhttp.send(null)
}
//codeforIE
elseif(window.ActiveXObject)
{
xmlhttp=newActiveXObject("Microsoft.XMLHTTP")
if(xmlhttp)
{
xmlhttp.onreadystatechange=state_Change
xmlhttp.open("GET",url,true)
xmlhttp.send()
}
}
}
functionstate_Change()
{
//ifxmlhttpshows"loaded"
if(xmlhttp.readyState==4)
{
//if"OK"
if(xmlhttp.status==200)
{
document.getElementById('T1').innerHTML=xmlhttp.responseText
}
else
{
alert("Problemretrievingdata:"+xmlhttp.statusText)
}
}
}
</script>
</head>
<bodyonload="loadXMLDoc('test_xmlhttp.txt')">
<divid="T1"style="border:1pxsolidblack;height:40;width:300"></div><br/>
<buttononclick="loadXMLDoc('test_xmlhttp2.txt')">Click</button>
</body>
</html>
更多关于ajax相关内容感兴趣的读者可查看本站专题:《jquery中Ajax用法总结》、《JavaScript中ajax操作技巧总结》、《PHP+ajax技巧与应用小结》及《asp.netajax技巧总结专题》。
希望本文所述对大家ajax程序设计有所帮助。
