首页 > Web开发 > 详细

ajax

时间:2015-06-29 13:09:28      阅读:188      评论:0      收藏:0      [点我收藏+]
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<style>
</style>
<script>
function ajax(url, fnSucc, fnFaild)
{
//1.创建ajax
if(window.XMLHttpRequest)
{
var oAjax=new XMLHttpRequest();
}
else
{
var oAjax=new ActiveXObject(‘Microsoft.XMLHttp‘);
}
//2.连接-open
oAjax.open(‘GET‘, url, true);
/*****************************
xmlHttp.open("POST",url,true);
        xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
        xmlHttp.send(parameter);
*****************************/
//3.发送-send
oAjax.send();
//4.接收-onreadystatechange
oAjax.onreadystatechange=function ()
{
if(oAjax.readyState==4)
{
//成功?
if(oAjax.status>=200 && oAjax.status<300 || oAjax.status==304)
{
//alert(‘成功:‘+oAjax.responseText);
fnSucc && fnSucc(oAjax.responseText);
}
else
{
//alert(‘失败‘);
fnFaild && fnFaild();
}
}
};
}
window.onload=function ()
{
var oBtn=document.getElementById(‘btn1‘);
oBtn.onclick=function ()
{
ajax(‘a.txt?t=‘+Math.random(), function (str){
alert(str);
}, function (){
alert(‘失败‘);
});
};
};
</script>
</head>
<body>
<input type="button" value="aaa" id="btn1" />
</body>
</html>

ajax

原文:http://www.cnblogs.com/xiaoleidiv/p/4607192.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!