Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:前端不死, 异步永生, 好好学
//创建Ajax请求对象
var ajax = new XMLHttpRequest();
//监听请求回调
ajax.onreadystatechange = function () {
if (ajax.readyState === 4 && ajax.status === 200) {
alert(ajax.responseText);
}
};
//初始化请求参数
ajax.open("GET", "homework1.php?name=taier&pwd=123456", true);
//发起请求
ajax.send();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Ajax请求</title>
</head>
<body>
<button class="get" onclick="getRequest(ajax);">GET请求</button>
<button class="post" onclick="postRequest(ajax);">模拟表单POST请求</button>
<button class="post" onclick="postRequest1(ajax);">
json字符串POST请求
</button>
<button class="post" onclick="postRequest2(ajax);">
FormData数据POST请求
</button>
<script>
//创建Ajax请求对象
var ajax = new XMLHttpRequest();
//监听请求回调
ajax.onreadystatechange = function () {
if (ajax.readyState === 4 && ajax.status === 200) {
alert(ajax.responseText);
}
};
//初始化请求参数open(请求类型,请求地址,是否异步)
//get请求
function getRequest(ajax) {
ajax.open("GET", "homework1.php?name=taier&pwd=123456", true);
ajax.send();
}
//post请求,
function postRequest(ajax) {
ajax.open("POST", "homework1.php", true);
//设置请求头,模拟表单方式发送数据
ajax.setRequestHeader(
"content-type",
"application/x-www-form-urlencoded"
);
var user = { name: "taier", pwd: "789123" };
var data = JSON.stringify(user); //转换为json字符串
ajax.send(data);
}
function postRequest1(ajax) {
ajax.open("POST", "homework1.php", true);
//设置请求头,json方式发送数据,php端以`php://input`流文件方式接收
ajax.setRequestHeader("content-type", "application/json;charset=utf-8");
var user = { name: "taisan", pwd: "00000" };
var data = JSON.stringify(user); //转换为json字符串
ajax.send(data);
}
function postRequest2(ajax) {
ajax.open("POST", "homework1.php", true);
//FormData方式发送数据
var data = new FormData();
// 添加数据 -->
data.append("name", "taisi");
data.append("pwd", "253512");
ajax.send(data);
}
//
</script>
</body>
</html>
<?php
if(!empty($_GET)){
//获取get请求的参数
echo json_encode($_GET);
}elseif(!empty($_POST)){
//以模拟表单和formData方式发送的可以用$_POST接收到
//模拟表单发送的数据在$_POST数组的一个键中
//formData方式发送的在$_POST数组中是键值对
echo json_encode($_POST);
}else{
//php://input 接收到的直接是字符串
$data = file_get_contents('php://input');
//$data = json_decode($data);
echo json_encode($data);
}
效果图
get请求发送的参数在$_GET数组中可以获取
POST模拟表单请求发送的参数在$_POST数组的一个键中
JSON字符串方式发送的通过php://input
流文件的方式获取
formData发送的参数在$_POST数组中直接获取
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>登录</title>
<link rel="stylesheet" type="text/css" href="login.css" />
</head>
<body>
<form onsubmit="return false" class="form">
<section>
<label for="username">账号:</label
><input type="text" name="username" id="username" required/>
</section>
<section>
<label for="password">密码:</label
><input type="password" name="password" id="password" required/>
</section>
<section>
<span id="res"></span>
</section>
<section>
<button onclick="login();">登录</button>
<span>没有账号?点击<a href="">注册</a>
</section>
</form>
<script>
function login(){
var ajax = new XMLHttpRequest();
//监听请求回调
ajax.onreadystatechange = function () {
if (ajax.readyState === 4 && ajax.status === 200) {
$data = JSON.parse(ajax.responseText);
//把服务器返回的结果写入到span标签中
document.getElementById('res').innerHTML=$data.msg;
}
};
ajax.open("POST", "login.php", true);
//FormData方式发送数据
var data = new FormData();
//获取input标签中输入的数据
var username = document.getElementsByName("username")[0].value;
var password = document.getElementsByName("password")[0].value;
//判断是否有输入用户名和密码
if(username===''||password===''){
document.getElementById('res').innerHTML='请输入用户名和密码';
return false;
}
console.log(username,password);
//添加到formData中
data.append("username",username);
data.append("password",password);
ajax.send(data);
}
</script>
</body>
</html>
页面样式文件:login.css
body {
display: grid;
justify-content: center;
align-items: center;
background-image: url("bg1.jpg");
background-size: 100vw 100vh;
}
.form {
margin-top: 220px;
width: 500px;
height: 300px;
background-color: #d7e6f1;
border-radius: 5%;
display: flex;
flex-flow: column nowrap;
justify-content: space-around;
}
section {
padding: 0 30px;
display: flex;
align-items: center;
justify-content: center;
}
section > input {
height: 30px;
flex-grow: 1;
font-size: 1.3em;
}
section > label,
button {
width: 80px;
font-size: 1.3em;
/* flex-grow: 3; */
}
.form > :last-of-type {
margin-left: 30px;
display: flex;
align-items: center;
justify-content: space-evenly;
}
#res {
color: red;
}
<?php
$data=[];
if(isset($_POST['username'])){
//把用户名和密码放到data数组中
$data[] = $_POST['username'];
$data[] = $_POST['password'];
//使用?占位符
$sql = "select count(*) from user where `username`=? and `password`=?";
$config = require('../database.php');//获取数据库配置参数
$pdo = new PDO($config['dsn'],$config['username'],$config['password']);
$stmt = $pdo->prepare($sql);//预处理
$stmt->execute($data);
$res = $stmt->fetch(PDO::FETCH_NUM);//获取索引数组结果集
//根据查询结果返回数据
if($res[0]==1){
//---显示返回结果,没有做跳转
echo json_encode(['code'=>1,'msg'=>'登录成功']);
}else{
echo json_encode(['code'=>0,'msg'=>'用户名或密码错误']);
}
}else{
echo json_encode(['code'=>404,'msg'=>'系统错误']);
}