javascript结合ajax读取txt文件内容
代码很简洁,这里就不多废话了,直接上源码
html代码
代码如下:
<!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>
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
内容随便填写
相关推荐
kentrl 2020-11-10
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
ajaxyan 2020-11-09
zndy0 2020-11-03
学留痕 2020-09-20
Richardxx 2020-11-09
learningever 2020-09-19
chongxiaocheng 2020-08-16
ajaxhe 2020-08-16
lyqdanang 2020-08-16
curiousL 2020-08-03
TONIYH 2020-07-22
时光如瑾雨微凉 2020-07-19
83510998 2020-07-18
坚持着执着 2020-07-16
jiaguoquan00 2020-07-07
李永毅 2020-07-05
坚持着执着 2020-07-05