ajax应用实例 获取各种数据
2016-03-06 12:48
375 查看
<script type="text/javascript"> window.onload = function(){ var oInp = document.getElementById("inp"); oInp.onclick = function(){ //获取text中的数据 // ajax("test.txt",function(data){ // console.log(data); // }) //获取数组中的数据 // ajax("test2.txt",function(data){ // //此时获取的不是数组,从服务器上取到的数据都是字符串 // //所以要对字符串进行操作 方便取到数据 //// console.log(data); // var arr = eval(data); // alert(arr[0]);//通过eval可以将字符串变成数组 // }) //获取数组中的json数据 把获取的数据输入到li中 var oUl = document.getElementById("ul1"); ajax("arrjson.txt",function(data){ //找到返回的事字符串 把字符串变成数组 var arr = eval(data); //找到数据后遍历数组 for(var i = 0 ; i <arr.length;i++ ) { var oLi = document.createElement("li"); oLi.innerHTML = '用户名为:<strong>'+arr[i].username+'</strong>,密码为:<i>'+arr[i].password+'</i>'; oUl.appendChild(oLi); } }) } } function ajax(url,fnusucc,fnFaild){ try{ var xhr = new XMLHttpRequest(); }catch(e){ var xhr = new ActiveXObject("Microsoft.XMLHTTP"); } xhr.open("get",url,true); xhr.send() xhr.onreadystatechange = function(){ if(xhr.readyState == 4) { if(xhr.status == 200) { fn(xhr.responseText); }else{ if(fnFaild) { fnFaild(xhr.status); } // alert("Err:"+xhr.status); } } } } </script>
<input type="button" name="" id="inp" value="获取" /> <ul id="ul1"> </ul>
test.txt
hello world
test2.txt
[1,2,3,4,5]
arrjson.txt
[{"username":"qq","password":"1234"},{"username":"as","password":"1234"},{"username":"zx","password":"1234"}]
相关文章推荐
- RTSP服务器实例live555源代码分析
- Hello Java
- win10 专业版 wamp 安装报错
- 浅读构建之法的提问以及阅读计划
- C++中头文件嵌套定义问题
- C++实现RTMP协议发送H.264编码及AAC编码的音视频
- [android] 采用断点调试的方式观察pull解析的流程
- Node.js模块封装及使用方法
- Codeforces Round #305 (Div. 2) D.Mike and Feet
- 关于内存
- 怎么读取二代身份证UUID----在STM32+CLRC663平台试验成功
- c++中的友元函数
- java学习笔记(二)关于main方法
- Codeforces 106C Buns 【0-1背包】
- 记一次深刻的教训-----将mat数据转化为SequenceFile
- 构建之法阅读笔记01
- hdoj 5233 Gunner II 【二分】
- HDU 2048 数塔
- Android6的Logger日志系统
- mysqldump 参数说明