let xhr = new XMLHttpRequest(); // 声明ajax对象
xhr.open('get', './get.php?username=xxx&password=yyy', true); // 预设get请求方式、拼接查询字符串的地址、是否异步
xhr.send(); // 向服务器发送请求
xhr.onreadystatechange = function() { // 等待数据相应
if (xhr.readyState === 4 && xhr.status === 200) { // 解析完成且状态码为200时,进行后续操作
console.log(xhr.responseText);
}
let xhr = new XMLHttpRequest(); // 声明ajax对象
xhr.open('post', 'post.php', true); // 预设post请求方式,要访问的地址,是否异步
xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded'); // 设置请求头部,用来解析传入的参数
xhr.send('username=xxx&password=yyy'); // 向服务器发送请求,需要传入queryString
xhr.onreadystatechange = function () { // 等待数据响应
if (xhr.readyState === 4 && xhr.status === 200) { // 解析完成且状态码为200时,进行后续操作
console.log(xhr.responseText);
}
}