15. Ajax
小于 1 分钟
15. Ajax
Ajax 简易实现:
export function getJSON(url) {
return new Promise((resolve, reject) => {
const xhr = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP')
xhr.open('GET', url, false)
xhr.setRequestHeader('Accept', 'application/json')
xhr.onreadystatechange = function () {
if (xhr.readyState !== 4)
return
if (xhr.status === 200 || xhr.status === 304)
resolve(xhr.responseText)
else
reject(new Error(xhr.responseText))
}
xhr.send()
})
}