原生 发送 POST 请求

在 html 中设置一个按钮

<button id='btn'> 按钮 <button>

 在 JavaScript 中获取 点击请求 post

let btn = document.getElementById('btn')

btn.onclick = () =>{
  let xhr = new XMLHttpRequest()
    xhr.open('post','url')
    xhr.send()
    xhr.onreadystatechange = function () {
        if(xhr.status == 200 && xhr.readyState == 4){
            console.log(xhr.responseText)
        }
    }
}

 

posted @ 2022-07-27 20:53  生活在北极的企鹅  阅读(37)  评论(0编辑  收藏  举报