get请求ajax
//get请求 const xhr = new XMLHttpRequest() xhr.open("GET","/api",false)//false表示请求方式为异步 xhr.onreadystatechange = function () { if(xhr.readyState === 4){ if(xhr.status === 200){ console.log(JSON.parse(xhr.responseText))//转化成json格式 } } } xhr.send(null)
post请求json
const xhr = new XMLHttpRequest() xhr.open("POST","/login",false) xhr.onreadystatechange = function () { if(xhr.readyState === 4){ if(xhr.status === 200){ console.log(JSON.parse(xhr.responseText)) } } } const postData = { username:'张三', password:'123456' } xhr.send(JSON.stringify(postData))//发送字符串