XMLHttpRequest函数封装

XMLHttpRequest函数封装:

function ajax(Url,sccuessFn,failureFn) {
	//1.创建XMLHttpRequest对象
	var xhr = null;
	if(window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	}else {
		xhr = new ActiveXObject('Microsoft.XMLHTTP');
	}
	//2.打开与服务器的链接
	xhr = open('get',Url,true);
	//3.发送给服务器
	xhr = send(null);
	//4.响应就绪
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4) {
			if(xhr.status == 200) {
				successFn(xhr.responseText);
			}else {
				if(failureFn) {
					failureFn();
				}else {
					alert(xhr.status);
				}
			}
		}
	}
}

  

原文地址:https://www.cnblogs.com/handsomehan/p/5868531.html