function Ajax(obj) {
//1.创建对象
var xhr = new XMLHttpRequest();
//判断get or post
if (obj.method.toUpperCase() == "GET") {
//2.连接
url = obj.data ? obj.url + "?" + obj.data : obj.url; //请求参数,如果有就拼接到后面
xhr.open(obj.method, url); //默认是true 异步
//3.发送请求
xhr.send();
} else {
xhr.open(obj.method, obj.url); //默认是true 异步
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(obj.data);
}
//4.监听
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
obj.success && obj.success(JSON.parse(xhr.responseText));
}
}
}
}
Ajax({
'method': "get",
'url': "bendi.json",
'success': function(data) {
console.log(data);
},
'data': ''
});
来源:CSDN
作者:Mrs彬
链接:https://blog.csdn.net/bin_2659017380/article/details/103927838