js:
open
方法用于初始化一个HTTP请求,它指定要发送的请求的类型、URL和是否使用异步方式发送请求。
xhr.open(method, url[, async[, user[, password]]]);
method
:请求的类型,如 "GET"、"POST"等。url
:请求的URL。async
:一个可选的布尔值,指示是否使用异步方式发送请求。默认为true,即异步。user
和password
:可选的用户名和密码,用于进行HTTP基本认证。
send
方法用于实际发送HTTP请求。一旦使用open
方法初始化了请求,你可以使用send
方法来发送请求并传递任何需要发送的数据。
xhr.send([data]);
data
:可选参数,表示要发送到服务器的数据,比如在POST请求中的表单数据。
function getHello(){
const xmlHttpRequse=new XMLHttpRequest();
//请求来了需要做什么
xmlHttpRequse.onreadystatechange=function(){
if (xmlHttpRequse.readyState==4){
if (xmlHttpRequse.status==200){
document.getElementById("test").innerText=xmlHttpRequse.responseText;
}
}
}
//发送请求
xmlHttpRequse.open("get","ajax");
xmlHttpRequse.send();
}
JQuery:
直接通过Ajax链接
$(function(){
//加一个选择器
$("选择器").click(function(){
$.ajax(
url:"xxxxx",
method:"get/post",
date:{
//发送的数据,如post发送数据和表单的数据
},
success:function(res){
//成功后的回调
}
)
})
})