博客列表 >Ajax请求与FormData

Ajax请求与FormData

phpcn_u202398
phpcn_u202398原创
2020年05月25日 15:18:00904浏览

1、Ajax请求

1.1、GET请求

代码示例

前端代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title></title>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. </head>
  8. <body>
  9. <script>
  10. //创建ajax对象
  11. var xhr = new XMLHttpRequest();
  12. //监听请求
  13. xhr.onreadystatechange = function(){
  14. if(xhr.readyState === 4 && xhr.status === 200){
  15. console.log(xhr.responseText);
  16. }
  17. };
  18. xhr.open("GET","get.php",true);
  19. xhr.send(null);
  20. </script>
  21. </body>
  22. </html>

后端数据

  1. <?php
  2. $user['name'] = "zcx";
  3. $user['pwd'] = "123456";
  4. echo json_encode($user);
  5. ?>

1.2、POST请求

application/x-www-form-urlencoded方法代码示例

前端代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title></title>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. </head>
  8. <body>
  9. <script>
  10. //创建ajax对象
  11. var xhr = new XMLHttpRequest();
  12. //监听请求
  13. xhr.onreadystatechange = function(){
  14. if(xhr.readyState === 4 && xhr.status === 200){
  15. console.log(xhr.responseText);
  16. }
  17. };
  18. xhr.open("POST","post.php",true);
  19. xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
  20. var user={
  21. name:"zcx",
  22. pwd:"123456",
  23. };
  24. var data = JSON.stringify(user);
  25. xhr.send(data);
  26. </script>
  27. </body>
  28. </html>

后端代码

  1. <?php
  2. $user = key($_POST);
  3. echo $user;
  4. $data = json_decode($user);
  5. print_r($data);
  6. $data = json_decode($user,true);
  7. print_r($data);
  8. ?>

application/x-www-form-urlencoded方法代码示例

前端代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title></title>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. </head>
  8. <body>
  9. <script>
  10. //创建ajax对象
  11. var xhr = new XMLHttpRequest();
  12. //监听请求
  13. xhr.onreadystatechange = function(){
  14. if(xhr.readyState === 4 && xhr.status === 200){
  15. console.log(xhr.responseText);
  16. }
  17. };
  18. xhr.open("POST","post1.php",true);
  19. xhr.setRequestHeader('Content-type','application/json;charset=utf-8');
  20. var user={
  21. name:"zcx",
  22. pwd:"123456",
  23. };
  24. var data = JSON.stringify(user);
  25. xhr.send(data);
  26. </script>
  27. </body>
  28. </html>

后端代码

  1. <?php
  2. $user = file_get_contents('php://input');
  3. echo $user;
  4. $data = json_decode($user);
  5. print_r($data);
  6. $data = json_decode($user,true);
  7. print_r($data);

1.3、FormData

代码示例

前端代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title></title>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <link href="" rel="stylesheet">
  8. </head>
  9. <body>
  10. <script>
  11. //创建ajax对象
  12. var xhr = new XMLHttpRequest();
  13. //监听请求
  14. xhr.onreadystatechange = function(){
  15. if(xhr.readyState === 4 && xhr.status === 200){
  16. console.log(xhr.responseText);
  17. }
  18. };
  19. xhr.open("POST","post2.php",true);
  20. var data = new FormData();
  21. data.append("name",'zcx')
  22. data.append("pwd",'123456')
  23. xhr.send(data);
  24. </script>
  25. </body>
  26. </html>

后端代码

  1. <?php
  2. $user['name'] = $_POST['name'];
  3. $user['pwd'] = $_POST['pwd'];
  4. print_r($user);

2、FormData完成用户登录与验证

代码示例

前端代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=<device-width>, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. body{
  9. box-sizing: border-box;
  10. margin: 0px;
  11. padding: 0px;
  12. }
  13. form{
  14. width: 260px;
  15. margin: auto;
  16. margin-top: 50px;
  17. background-color: #a7c4e0;
  18. padding: 30px 30px;
  19. display: grid;
  20. gap: 20px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <form action="" method="POST" onsubmit="return false">
  26. <div>
  27. <label>用户名:</label>
  28. <input type="text" name="name" placeholder="请输入用户名" autofocus required >
  29. </div>
  30. <div>
  31. <label>密 码:</label>
  32. <input type="password" name="pwd" placeholder="请输入用户名" required>
  33. </div>
  34. <div style="margin:auto;">
  35. <button>登 录</button>
  36. </div>
  37. </form>
  38. </body>
  39. </html>
  40. <script>
  41. var form = document.querySelector("form");
  42. var btn = document.querySelector("form button");
  43. btn.onclick = function(){
  44. //创建ajax对象
  45. var xhr = new XMLHttpRequest();
  46. //监听请求
  47. xhr.onreadystatechange = function(){
  48. if(xhr.readyState === 4 && xhr.status === 200){
  49. var res = JSON.parse(xhr.responseText);
  50. switch(res.status){
  51. case 0:
  52. error = res.message;
  53. break;
  54. case 1:
  55. error = res.message;
  56. break;
  57. default:
  58. error="未知错误";
  59. }
  60. var span = document.createElement("span");
  61. span.innerHTML = error;
  62. span.style.color ="red";
  63. form.appendChild(span);
  64. }
  65. };
  66. xhr.open("POST","index.php",true);
  67. var data = new FormData(form);
  68. xhr.send(data);
  69. };
  70. var inputs = document.querySelectorAll("input");
  71. for(var i = 0;i < inputs.length; i++){
  72. inputs[i].oninput = function(){
  73. if(btn.nextElementSibling !== null) form.removeChild(btn.nextElementSibling)
  74. };
  75. }
  76. </script>

后端代码

  1. <?php
  2. $pdo = new PDO("mysql:host=localhost;dbname=xxyl","root","root");
  3. $sql = "select count(`id`) from `users` where `name`=? and `pwd`=? limit 1";
  4. $stmt = $pdo->prepare($sql);
  5. $stmt->execute([$_POST['name'],md5($_POST['pwd'])]);
  6. $res = $stmt->fetch(PDO::FETCH_NUM);
  7. if($res[0]== 1){
  8. echo json_encode(['status'=>0,'message'=>'验证成功']);
  9. }else{
  10. echo json_encode(['status'=>1,'message'=>'用户名或密码错误']);
  11. }

" class="reference-link">

学习总结

本节课我们学习了Ajax与FormData的知识,通过本节课的学习对POST提交数据的方式有了新的认识,学到了FormData的知识,本知识都是以前不知道。通过以后的学习用于综合实践。

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议