异步的JavaScript和XML,AJAX 不是新的编程语言,而是一种使用现有标准的新方法

    AJAX 最大的优点是在不重新加载整个页面的情况下,可以与服务器交换数据并更新部分网页内容。

    使用

    1. // 要发送的数据
    2. const data = {}
    3. // 生成xhr对象
    4. const xhr = new XMLHttpRequest()
    5. xhr.onreadystatechange = () => {
    6. if (xhr.readyState === 4 && xhr.status === 200) {
    7. const res = xhr.response
    8. console.log(res)
    9. }
    10. }
    11. xhr.open('GET','/ajax/path')
    12. // open(method, url, async = true, username, password)
    13. xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    14. xhr.send(data)