-
Notifications
You must be signed in to change notification settings - Fork 6
Open
Description
任务:
手写实现 AJAX
代码:
/**
* @param {string} url - 请求的url地址
* @param {string} method - 请求方式,get(默认) || post
* @param {function} callback - 回调函数
*/
function sendAjaxRequest(url, method, callback) {
var xhr = null;
method = method || 'get';
callback = typeof callback === 'function' ? callback : function() {};
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else {
xhr = new ActiveObject('Microsoft.XMLHTTP'); // 兼容低版本IE
}
xhr.open(method, url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var data = JSON.parse(xhr.responseText);
callback(data);
}
}
}
xhr.send(null);
}Metadata
Metadata
Assignees
Labels
No labels