不会建立本地服务器的小伙伴,可以看一下主页另一篇文章
使用node.js的express建立本地服务器
axo(
{
url: 'http://127.0.0.1:8080/api/login',
method: 'post',
data: {
username: '赵璐',
password: '123456'
}
}
)
function resolveDate(data) {
let arr = []
for (let k in data) {
let str = k + '=' + data[k]
arr.push(str)
}
return arr.join('&')
}
function axo(option) {
let qs = resolveDate(option.data)
let xhr = new XMLHttpRequest()
if (option.method.toUpperCase() == 'GET') {
xhr.open(option.method, option.url + '?' + qs)
xhr.send()
} else if (option.method.toUpperCase() == 'POST') {
xhr.open(option.method, option.url)
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
xhr.send(qs)
} else {
return console.log('请求方式错误')
}
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
let result = JSON.parse(xhr.responseText)
console.log(result);
}
}
}