JavaScript基础

AJAX五步

2018-12-01  本文已影响0人  雪妮爸爸

AJAX五步

1.创建一个异步对象。
xmlhttp=new XMLHttpRequest();
2.设置请求方式和请求地址。

规定请求的类型、URL 以及是否异步处理请求。
method:请求的类型;GET 或 POST
url:文件在服务器上的位置
async:true(异步)或 false(同步)

xmlhttp.open('G','04-ajax-get.php',true);
3.发送请求。
xmlhttp.send();
4.监听状态的变化。


image.png

5.处理返回的结果。

window.onload = function () {
        var oBtn = document.getElementsByTagName('button')[0];
        oBtn.onclick = function () {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open('G','04-ajax-get.php',true);
            xmlhttp.send();
            xmlhttp.onreadystatechange = function () {
                if(xmlhttp.readyState == 4) {
                    if((xmlhttp.status >= 200 && xmlhttp.status <= 300) || xmlhttp.status == 304)
                    {console.log('接受到返回的数据');
                    } else{console.log('没有接受到返回的数据');}
                }
            }
        }
    }
上一篇 下一篇

猜你喜欢

热点阅读