ajax读取txt文件内容(方法2)
发布时间:2020-12-16 02:08:01 所属栏目:百科 来源:网络整理
导读:!doctype html html head meta charset="utf-8"/ /head body button type="button" onclick="show()"请求数据/button script src="ajax.js"/script script function show(){ Ajax('read.txt?datetime=new Date.getTime ',function(str){alert(str);},functio
<!doctype html> <html> <head> <meta charset="utf-8"/> </head> <body> <button type="button" onclick="show()">请求数据</button> <script src="ajax.js"></script> <script> function show(){ Ajax('read.txt?datetime=new Date.getTime ',function(str){alert(str);},function(){alert('失败了');}) }; </script> </body> </html> js调用方法: 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); } }; }; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |