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 原理 #32

Open
wengjq opened this issue Mar 3, 2018 · 0 comments
Open

ajax 原理 #32

wengjq opened this issue Mar 3, 2018 · 0 comments

Comments

@wengjq
Copy link
Owner

wengjq commented Mar 3, 2018

function ajax() {
    var xmlhttp;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = ActiveXObject("Microsoft.XMLHTTP");
    }

    //判定执行状态
    xmlhttp.onreadystatechange = function () {
        /*
        readyState
            0: 请求未初始化
            1: 服务器连接已建立
            2: 请求已接收
            3: 请求处理中
            4: 请求已完成,且响应已就绪
        status
            200: 请求成功
            404: 未找到
            500: 服务器内部错误
        */
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("myDiv").innerHTML = xmlhttp.responseText; //获得字符串形式的响应数据
        }
    }

    xmlhttp.open("Get", "url", true);

    //设置头信息
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    //将信息发送到服务器
    xmlhttp.send();    
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant