美文网首页
JavaScript 原生 AJAX

JavaScript 原生 AJAX

作者: 你听得到_ccoc | 来源:发表于2019-08-24 18:11 被阅读0次

    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) {
                //出错
            }
    

    相关文章

      网友评论

          本文标题:JavaScript 原生 AJAX

          本文链接:https://www.haomeiwen.com/subject/ehbqectx.html