function postStr() {
//通过Ajax的get请求获取后端数据(无刷新)
//1.创建XMLHttpRequset对象
var xhr = new XMLHttpRequest();
//2.打开请求服务器的地址(get请求将参数放到URL前)
xhr.open("post", "/Home/PostString");
//post请求修改请求头
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//3.发送一个Http请求
xhr.send("age=100&name=231");
//4.监听响应请求的状态,然后执行对应的方法
xhr.onreadystatechange = function () {
//onreadystatechange表示ajax请求状态码
//State表示请求状态码
if (xhr.readyState == 4 && xhr.status == 200) {
var str = xhr.responseText;
window.document.getElementById("div1").innerHTML = str;
}
}
}
Ajax基础(Post)
最新推荐文章于 2024-07-25 12:41:07 发布