const xhr = new XMLHttpRequest();
xhr.open("post", "http://www.wyhyit.cn:8080/yjsystem/data/list");
//负载application/x-www-form-urlencoded方式
// xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// xhr.send("page=1&isDeal=1");
xhr.setRequestHeader("Content-type", "application/json");
xhr.responseType = 'json'; //告诉浏览器返回json格式数据
let postdata = {page:1,isDeal:1};
// xhr.send(JSON.stringify(postdata)); //负载转为字符串
xhr.send(postdata); //直接这样用也行,负载查看就是object
console.log(xhr.readyState);//1 服务器连接已建立
xhr.onreadystatechange = function () {
console.log(xhr.readyState);//2 3 4
if (xhr.readyState == 4 && xhr.status == 200) {
// console.log(xhr.responseText); //返回文本形式
console.log(xhr.response);
}
}
javascript ajax 设置请求和响应格式
于 2024-03-21 17:26:37 首次发布