用JS封装AJAX请求 XMLHttpRequest (面试前记得复习)

来源:互联网 发布:淘宝关键词搜索 编辑:程序博客网 时间:2024/05/21 18:42
function Ajax(type, url, param, onsucess) {    var xhr;    if (XMLHttpRequest) {        xhr = new XMLHttpRequest();    } else {        xhr = ActiveXObject("XMLHttpRequest");    }    if (type == "Get" || type == "get") {        xhr.open("Get", url, true);    } else {        xhr.open("Post", url, true);        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    }    xhr.send(param);    xhr.onreadystatechange = function (data) {        if (xhr.readyState == 4) {            if (xhr.status == 200) {                onsucess(xhr.responseText);            }        }    };}


0 0
原创粉丝点击