AJAX-post-get simple package

Ajax function (Options) {
     // 1. The default process parameters 
    var {type, URL, Success, error, Data, timeout} = Options; 
    type = type || " GET " ; 
    Data = Data || {}; 
    timeout = timeout || 2000 ; 

    // data to be transmitted is parsed 2. 
    var STR = "" ;
     for ( var I in data) { 
        STR + = {I} = $ `$ {data [I]} & `; 
    } 

    // . 3 according to the way, the decision whether to process url 
    iF (of the type == " GET ") {
         Var D = new new ; a Date () 
        URL = URL + " ? " + STR + " __qft = " + d.getTime (); 
    } 

    // 4. Open Ajax 
    var XHR = new new the XMLHttpRequest ();
     // NOTE: open in such a way 
    xhr.open (type, URL, to true ); 
    xhr.onreadystatechange = function () {
         IF (xhr.readyState == . 4 && xhr.status == 200 is ) {
             // 5. before executed successfully, the first determination whether incoming 
            success &&Success (xhr.responseText);
             // after successful, there should be a failure 
            error = null ; 
        } the else  IF (xhr.readyState == . 4 && xhr.status =! 200 is ) {
             // 6. Before fails, first determine whether incoming 
            error && error (xhr.status);
             // after the failure, there should be no success 
            success = null ;
             // and should not be performed repeatedly failed 
            error = null ; 
        } 
    } 

    // 7. The If the request times out, fails 
    setTimeout (() => { 
        errorError && ( " timeout " );
         // after the failure, there should be no success 
        Success = null ; 
    }, timeout); 

    // 8. The last type of embodiment according to the determined format and transmits the content send the 
    IF (type == " POST " ) { 
        xhr.setRequestHeader ( " the Content-type " , " file application / X-WWW-form-urlencoded " ); 
        xhr.send (STR) 
    } the else { 
        xhr.send () 
    } 
}

 

Guess you like

Origin www.cnblogs.com/stdzz/p/11515466.html