function ajax(opt) {
let defult_obj = {
url: '',
method: 'GET',
async: true,
data: {
pageSize: 5,
pageNum: 0,
},
success: null,
}
let newOpt = Object.assign(defult_obj, opt);
let xhr = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
console.log(xhr.responseText)
}
}
let search = '';
if (newOpt.method.toUpperCase() === "GET") {
search = '?' + Object.keys(newOpt.data).map(function (k) {
return k + '=' + newOpt.data[k];
}).join('&');
console.log(search)
}
xhr.open(newOpt.method, newOpt.url + search, newOpt.async);
let data = null;
if (newOpt.method.toUpperCase() === "POST") {
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
data = JSON.stringify(newOpt.data)
}
xhr.send(data);
}
ajax({
url: 'http://192.168.1.119:3000/api/data',
method: 'POST',
async: true,
data: {
pageSize: 10,
pageNum: 0,
},
success: function (data) {
}
})
网友评论