程式碼很簡潔,這裡就不多廢話了,直接上原始碼
html代碼
<script><br />
function show(){<br />
Ajax('read.txt?datetime=new Date.getTime ',function(str){alert(str);},function(){alert('失敗了');})<br />
};<br />
</script>
javascript程式碼
function Ajax(url,fnSucc,fnFaild)
{
//1.建立ajax物件
if(window.XMLHttpRequest)
{// code for IE7 , Firefox, Chrome, Opera, Safari
var oAjax=new XMLHttpRequest();
}
else
{// code for IE6, IE5
var oAjax=new ActiveXObject("Microsoft.XMLHTTP");
}
//2.以連結伺服器(開啟伺服器的連接)
//open(方法,且檔案名,且非同步傳送)
oAjax.open('GET',url,true);
//3.以
oAjax.send();
//4.接收與返回
oAjax.onreadystatechange=function()
{
if (oAjax.readyState==4)
{
if(oAjax.status==200)
{
fnSucc(oAjax.responseText);
}
else
{
fnFaild(oAjax.status);
}
};
};
}
請求的檔案為read.txt
內容隨便填入