get请求
var xhr = new XMLHttpRequest();
xhr.open("get",`./index.php`,true)
xhr.send();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
if(xhr.status == 200){
// xhr.responseText是php的返回值
console.log(xhr.responseText);
}
}
}
post请求
var xhr = new XMLHttpRequest();
xhr.open("post", "./index.php", true);
var fd = new FormData();
fd.append('zhang', zh.value);
xhr.send(fd);
xhr.onreadystatechange = function (){
if(xhr.readyState == 4){
if(xhr.status == 200){
console.log(xhr.responseText);
}
}
};
使用XMLHttpRequest进行GET和POST请求
这篇博客介绍了如何使用JavaScript的XMLHttpRequest对象来发送GET和POST请求。GET请求示例展示了如何加载并打印PHP返回的数据,而POST请求则演示了如何传递表单数据到PHP并接收响应。

被折叠的 条评论
为什么被折叠?



