• javasript之ajax学习笔记


    html代码

     1 <!doctype html>
     2 <html>
     3  <head>
     4      <meta charset="utf-8"/>
     5      </head>
     6      <body>
     7  
     8          <button type="button"  onclick="show()">请求数据</button>
     9         <script src="ajax.js"></script>
    10         <script>
    11           function show(){
    12 
    13             Ajax('read.txt?datetime=new Date.getTime ',function(str){alert(str);},function(){alert('失败了');})
    14            
    15           };
    16         </script>
    17  
    18      </body>
    19 
    20 </html>

    javascript代码

     1  function Ajax(url,fnSucc,fnFaild)
     2             {
     3                  //1.创建ajax对象
     4                if(window.XMLHttpRequest)
     5                 {// code for IE7+, Firefox, Chrome, Opera, Safari
     6                      var oAjax=new XMLHttpRequest();
     7                 }
     8                else
     9                 {// code for IE6, IE5
    10                     var oAjax=new ActiveXObject("Microsoft.XMLHTTP");
    11                 }
    12                 //2.链接服务器(打开服务器的连接)
    13                 //open(方法,文件名,异步传输)
    14                 oAjax.open('GET',url,true);
    15                 //3.发送
    16                 oAjax.send();
    17                 //4.接收返回
    18                 oAjax.onreadystatechange=function()
    19                    {
    20                      if (oAjax.readyState==4)
    21                      {
    22                        if(oAjax.status==200)
    23                          { 
    24                            fnSucc(oAjax.responseText);  
    25                          }
    26                       else
    27                          {
    28                           fnFaild(oAjax.status);
    29                          }
    30                         
    31                      };
    32                    
    33                 };
    34            
    35              }

    请求的文件为read.txt

    内容随便填写

    if you don't try,you will never know!
  • 相关阅读:
    抚琴弹唱东流水
    借点阳光给你
    日月成双行影单
    一夜飘雪入冬来
    悼念钱学森
    我的青春谁作主
    重游望江楼有感
    雪后暖阳
    满城尽添黄金装
    敢叫岁月不冬天
  • 原文地址:https://www.cnblogs.com/leeten/p/3498327.html
Copyright © 2020-2023  润新知