XMLHttpRequest函数封装

2023-04-24,,

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);
}
}
}
}
}

  

XMLHttpRequest函数封装的相关教程结束。

《XMLHttpRequest函数封装.doc》

下载本文的Word格式文档,以方便收藏与打印。