异步的JavaScript和XML,AJAX 不是新的编程语言,而是一种使用现有标准的新方法
AJAX 最大的优点是在不重新加载整个页面的情况下,可以与服务器交换数据并更新部分网页内容。
使用
// 要发送的数据
const data = {}
// 生成xhr对象
const xhr = new XMLHttpRequest()
xhr.onreadystatechange = () => {
if (xhr.readyState === 4 && xhr.status === 200) {
const res = xhr.response
console.log(res)
}
}
xhr.open('GET','/ajax/path')
// open(method, url, async = true, username, password)
xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhr.send(data)