JavaScript 原生 AJAX

2019-08-24  本文已影响0人  你听得到_ccoc

JavaScript 原生 ajax

注意浏览器兼容性判断


    var http = new XMLHttpRequest();
    http.open("method", "url", true);//第三个参数 async
    http.onreadystatechange = function () {
        //ajax 状态值判断
        if (http.readyState == "4") {
            //ajax 状态码判断
            if ((http.status >= 200 && http.status < 300) || http.status == 304){
                console.log(http.responseText);
            }
        }
        else {
            console.log(http.status);

        }
    }
    xml.send();

JQuery 类库的 ajax

    $.ajax({
        url: "url",
        data: "data",
        type: "method",
        dataType: "json",
        async: true,
        beforSend: function () {
            //请求之前

        },
        success: function (d) {
            //成功
        },
        error: function (e) {
            //出错
        }
上一篇 下一篇

猜你喜欢

热点阅读