AJAX请求

使用纯粹的JavaScript创建一个AJAX请求:

function loadXMLDoc() {

    var xmlhttp;

 

    if (window.XMLHttpRequest) {

        // code for IE7+, Firefox, Chrome, Opera, Safari

        xmlhttp = new XMLHttpRequest();

    } else {

        // code for IE6, IE5

        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

    }

 

    xmlhttp.onreadystatechange = function() {

        if (xmlhttp.readyState == 4 ) {

           if(xmlhttp.status == 200){

               alert("success");

           }

           else if(xmlhttp.status == 400) {

              alert("error 400")

           }

           else {

               alert("something broke")

           }

        }

    }

 

    xmlhttp.open("GET", "test.html", true);

    xmlhttp.send();

}

来源: Stack Overflow

而使用jQuery创建AJAX请求:

$.ajax({

    url: "test.html",

    statusCode: {

    200: function() {

    alert("success");

    },

    400: function() {

    alert("error 400");

    }

    },

    error: function() {

    alert("something broke");

    }

});

原文地址:https://www.cnblogs.com/papajia/p/4498200.html