Blogger Information
Blog 11
fans 0
comment 0
visits 7546
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS - Ajax基础
老陈
Original
612 people have browsed it

1.Ajax-GET

  1. //1.创建Ajax对象
  2. var xhr= new XMLHttpRequest();
  3. //2.监听请求
  4. xhr.onreadystatechange = function(){
  5. if(xhr.readyState ===4 && xhr.status ===200){
  6. console.log(xhr.responseText);
  7. }
  8. };
  9. //3.初始化请求参数
  10. // xhr.open(请求类型,请求URL, 是否异步);
  11. xhr.open("GET" ,"test.php",true);
  12. //4.发送请求
  13. xhr.send(null);
  1. //test.php 代码
  2. // 创建数组
  3. $user['name'] = 'laochen';
  4. $user['email'] = 'laochen@qq.cpm';
  5. // JSON 编码返回
  6. echo json_encode($user);

Ajax-post

  1. 方法一:
  2. //1.创建Ajax对象
  3. var xhr= new XMLHttpRequest();
  4. //2.监听请求
  5. xhr.onreadystatechange = function(){
  6. if(xhr.readyState ===4 && xhr.status ===200){
  7. console.log(xhr.responseText);
  8. }
  9. };
  10. //3.初始化请求参数
  11. xhr.open("POST" ,"test2.php",true);
  12. //4.设置请求头,模拟表单类型的数组进行发送
  13. xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
  14. //5.创建对象
  15. var user = {
  16. email : "laochen@qq.com",
  17. password :"555666",
  18. };
  19. var date = JSON.stringify(user);
  20. //6.发送请求
  21. xhr.send(date);
  22. 方法二:
  23. //json格式 需要设置utf-8
  24. //1.创建Ajax对象
  25. var xhr= new XMLHttpRequest();
  26. //2.监听请求
  27. xhr.onreadystatechange = function(){
  28. if(xhr.readyState ===4 && xhr.status ===200){
  29. console.log(xhr.responseText);
  30. }
  31. };
  32. //3.初始化请求参数
  33. xhr.open("POST" ,"test2.php",true);
  34. //4.设置请求头,模拟表单类型的数组进行发送
  35. xhr.setRequestHeader("content-type", " "application/json;charset=utf-8"");
  36. //5.创建对象
  37. var user = {
  38. email : "laochen@qq.com",
  39. password :"555666",
  40. };
  41. var date = JSON.stringify(user);
  42. //6.发送请求
  43. xhr.send(date);
  1. 方法一:
  2. $data = key($_POST);
  3. //将$date 转为php可以处理的数据
  4. //对象格式
  5. $user = json_decode($data);
  6. print_r($user);
  7. //数组格式
  8. $user = json_decode($data, true);
  9. print_r($user);
  10. 方法二:
  11. $data = file_get_contents('php://input');
  12. $user = json_decode($data);
  13. print_r($user);

FormData

1.可直接序列化表单数据,可直接被 Ajax 识别,2.所以可以不设置请求头,3.除了表单数据外,也可用于普通数据
  1. //1.创建Ajax对象
  2. var xhr= new XMLHttpRequest();
  3. //2.监听请求
  4. xhr.onreadystatechange = function(){
  5. if(xhr.readyState ===4 && xhr.status ===200){
  6. console.log(xhr.responseText);
  7. }
  8. };
  9. //3.初始化请求参数
  10. xhr.open("POST" ,"test4.php",true);
  11. //FormData
  12. var data = new FormData();
  13. data.append("username","admin");
  14. data.append("password","555666");
  15. //5.发送请求
  16. xhr.send(data);
  1. //test4.php 代码
  2. // 可以直接获取到值
  3. print_r($_POST);

FormData 用户登陆验证

  1. <p>邮箱登陆</p>
  2. <!-- onsubmit="return false": 禁用表单的默认提交,改为自定义的Ajax提交 -->
  3. <form
  4. action=""
  5. method="POST"
  6. style="display: grid; gap: 15px;"
  7. onsubmit="return false"
  8. >
  9. <input
  10. type="email"
  11. name="email"
  12. placeholder="email@email.com"
  13. required
  14. autofocus
  15. />
  16. <input type="password" name="password" placeholder="******" required />
  17. <button>提交</button>
  18. </form>
  19. <script>
  20. // 1. 获取表单和按钮
  21. var form = document.querySelector("form");
  22. var btn = document.querySelector("form button");
  23. // 2.钮绑定点击事件,进行Ajax请求
  24. btn.onclick = function () {
  25. // 1. 创建Ajax对象
  26. var xhr = new XMLHttpRequest();
  27. // 2. 监听请求
  28. xhr.onreadystatechange = function () {
  29. if (xhr.readyState === 4 && xhr.status === 200) {
  30. console.log(xhr.responseText);
  31. // 将jsonl转js对象
  32. var res = JSON.parse(xhr.responseText);
  33. console.log(res);
  34. switch (res.status) {
  35. case 0:
  36. case 1:
  37. error = res.message;
  38. break;
  39. default:
  40. error = "未知错误";
  41. }
  42. // 将提示显示到表单中
  43. var span = document.createElement("span");
  44. span.innerHTML = error;
  45. span.style.color = "red";
  46. form.appendChild(span);
  47. }
  48. };
  49. // 3. 初始化请求参数
  50. xhr.open("POST", "test4.php", true);
  51. // FormData
  52. var data = new FormData(form);
  53. data.append("login_time", new Date().getTime());
  54. // 5. 发送请求
  55. xhr.send(data);
  56. };
  57. // 清除提示信息
  58. var inputs = document.querySelectorAll("input");
  59. for (var i = 0; i < inputs.length; i++) {
  60. inputs[i].oninput = function () {
  61. if (btn.nextElementSibling !== null)
  62. form.removeChild(btn.nextElementSibling);
  63. };
  64. }
  65. </script>
  1. $pdo = new PDO('mysql:host=localhost;dbname=phpedu', 'root', 'root');
  2. $stmt = $pdo->prepare("SELECT COUNT(`id`) FROM `users` WHERE `email`=? AND `password`=? LIMIT 1");
  3. $stmt->execute([$_POST['email'], md5($_POST['password'])]);
  4. $user = $stmt->fetch(PDO::FETCH_NUM);
  5. if ($user[0] == 1){
  6. echo json_encode(['status'=>1, 'message'=>'验证通过']);
  7. }
  8. else {
  9. echo json_encode(['status'=>0, 'message'=>'邮箱或密码错误']);
  10. }

总结:没有彻底理解,但GET,POST,FormData的步骤都可以自己写出来了。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:没有完全理解, 就不要着急提交作业, 这样效果反而不好
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post