zoukankan      html  css  js  c++  java
  • 笔记-Ajax[3]-ajax类终极版;

    function Ajax(method,url,data,success){

        var xhr;
        //1:创建对象
        try {
          xhr = new XMLHttpRequest();
        } catch (e) {
          xhr = new ActiveXObject('Microsoft.XMLHTTP');
        }
    //2:建立请求
       if(method=='get'){
          if(data){
            url += '?' + data+"&"+new Date().getTime();//解决get方式的缓存问题
          }
          xhr.open(method,url,true);
       }else{
          xhr.open(method,url,true);
          xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');//申明发送的数据类型
       }

    //3:发送请求

      if(method=='get'){
        xhr.send();//当为get方式的时候这个参数为空
      }else{
        data?xhr.send(data):xhr.send();//当为post方式的时候这个参数为数据
      }

    //4:回应
    xhr.onreadystatechange = function() {

      if ( xhr.readyState == 4 ) {
        if ( xhr.status == 200 ) {
            success && success(xhr.responseText);//回调函数处理数据
        } else {
            alert('出错了哥们,Err:' +xhr.status);   

        }
      }
       }

    };

     调用 :

    Ajax('post','date.txt','',function(data){
      alert(data);
    });

  • 相关阅读:
    [Noi2011]阿狸的打字机
    Bzoj3530: [Sdoi2014]数数
    Bzoj2037: [Sdoi2008]Sue的小球
    Bzoj4869: [Shoi2017]相逢是问候
    Bzoj1899: [Zjoi2004]Lunch 午餐
    Bzoj3884: 上帝与集合的正确用法
    UVA10692:Huge Mods
    Bzoj1009: [HNOI2008]GT考试
    Bzoj1212: [HNOI2004]L语言
    【国家集训队2012】tree(伍一鸣)
  • 原文地址:https://www.cnblogs.com/hellome/p/3771085.html
Copyright © 2011-2022 走看看