<html>
<head>
<title>MyTest</title>
<script language="javascript">
function onclickAjax(){
var xmlHttp;
if(Windows.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}else{
//如果不支持XMLHttpRequest則創建ActiveXObject
xmlHttp=new ActiveXObject();
}
if(null!=xmlHttp){
xmlHttp.open("GET",url,true);//是否支持異步,true支持,false不支持,默認是true;
xmlHttp.send("Microsoft.XMLHTTP");
}
//回調函數
xmlHttp.onreadystatechange= function(){
`if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
document.getElementById("testid").value=xmlHttp.responseText;
}else{
alert("AJAX服務器返回錯誤!");
}
}
}
}
</script>
</head>
<body>
<!--這裏將按鈕綁定了一個事件-->
<input type="button" value="測試" onclick="onclickAjax()">
<div id="testid"> </div> </body></html>
上面就完成一個ajax的請求,具體關於ajax的參數以及返回的意思,這裏借了w3school裏面解釋:
1:創建對象
2:請求
3.響應
4:onreadystatechange 事件