function pa(method, url, data) {
return new Promise(function (resolve, reject) {
const xhr = new XMLHttpRequest();
const newmethod = method.toUpperCase();
xhr.open(newmethod, url, true);
if (method === 'POST') {
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
}
//level-1
// xhr.onreadystatechange = function () {
// if (this.readyState === 4) {
// if (this.status === 200) {
// resolve(this.responseText, this);
// } else {
// let res = {code: this.status, response: this.response}
// reject(res, this)
// }
// }
// };
if (method === 'POST') {
xhr.send(data)
} else {
xhr.send()
}
//level-2
xhr.onload = resolve;
xhr.onerror = reject; })}
pa(
'get',
'url') .
then((value) => {
console.
log(
'then', value) }) .
catch(error => {
console.
log(
'catch', error) })