码迷,mamicode.com
首页 > Web开发 > 详细

ajax

时间:2015-06-29 13:10:07      阅读:195      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!