• ajax代码及简单封装


     1  var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); //创建XMLHTTP对象,考虑兼容性
     2             xmlhttp.open("POST", "AJAXTest.ashx?" + "i=5&j=10", true); //“准备”向服务器的GetDate1.ashx发出Post请求(GET可能会有缓存问题)。这里还没有发出请求
     3             xmlhttp.onreadystatechange = function ()
     4             {
     5                 if (xmlhttp.readyState == 4) //readyState == 4 表示服务器返回完成数据了。之前可能会经历2(请求已发送,正在处理中)、3(响应中已有部分数据可用了,但是服务器还没有完成响应的生成)
     6                 {
     7                     if (xmlhttp.status == 200) //如果状态码为200则是成功
     8                     {
     9                         alert(xmlhttp.responseText);
    10                     }
    11                     else
    12                     {
    13                         alert("AJAX服务器返回错误!");
    14                     }
    15                 }
    16             }
    17 //不要以为if (xmlhttp.readyState == 4) {在send之前执行!!!!
    18             xmlhttp.send(); //这时才开始发送请求
    19 //发出请求后不等服务器返回数据,就继续向下执行,所以不会阻塞,界面就不卡了,这就是AJAX中“A”的含义“异步”。试着在ashx加一句Thread.Sleep(3000);
    20 
    21 简单的ajax封装:
    22  function ajax(url,onsuccess,onfail)
    23         {
    24             var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
    25             xmlhttp.open("POST", url, true);
    26             xmlhttp.onreadystatechange = function ()
    27             {
    28                 if (xmlhttp.readyState == 4)
    29                 {
    30                     if (xmlhttp.status == 200)
    31                     {
    32                         onsuccess(xmlhttp.responseText);
    33                     }
    34                     else
    35                     {
    36                         onfail(xmlhttp.status);
    37                     }
    38                 }
    39             }
    40             xmlhttp.send(); //这时才开始发送请求
    41         }
  • 相关阅读:
    eclipse3.2 汉化 汉化包下载
    JAXB 操作XML 与 Object
    具体解释三层架构图
    四个好看的CSS样式表格
    LinearGradient线性渲染
    JAVA wait(), notify(),sleep具体解释
    System.currentTimeMillis();
    nefu117 素数个数的位数,素数定理
    java jdk缓存-128~127的Long与Integer
    js正則表達式语法
  • 原文地址:https://www.cnblogs.com/Steven-shi/p/5129978.html
Copyright © 2020-2023  润新知