Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajax手写实现 #4

Open
law-chain-hot opened this issue Feb 17, 2020 · 0 comments
Open

Ajax手写实现 #4

law-chain-hot opened this issue Feb 17, 2020 · 0 comments
Labels

Comments

@law-chain-hot
Copy link
Owner

Ajax手写实现

0:未初始化 -- 尚未调用.open()方法;
1:启动 -- 已经调用.open()方法,但尚未调用.send()方法;
2:发送 -- 已经调用.send()方法,但尚未接收到响应;
3:接收 -- 已经接收到部分响应数据;
4:完成 -- 已经接收到全部响应数据,而且已经可以在客户端使用了;

代码

function ajax(url){
    // check IE
    let xmlhttp
    if (window.XMLHttRequest){
        xmlhttp = new XMLHttpRequest;
    } else {
        xmlhttp = new ActiveXObject('Mirosoft.HTTP')
    }

    // check status
    xmlhttp.onreadystatuschange = () => {
        if(xmlhttp.readystatus == 4 && xmlhttp.status == 200){
            // do it
        }
    }

    xmlhttp.open('GET', url, true) // get方法, URL,和 asycn调用为ture
    xmlhttp.send()

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant