博客列表 >异步请求这里学!实例演示Ajax-post的三种方式,小白都说:so easy....

异步请求这里学!实例演示Ajax-post的三种方式,小白都说:so easy....

张福根一修品牌运营
张福根一修品牌运营原创
2020年11月06日 15:47:27838浏览

实例演示Ajax-post的三种方式

方式一:通常方式

  1. <script>
  2. // 1. 创建请求对象: `new XMLHttpRequest()`
  3. const xhr = new XMLHttpRequest();
  4. // 2. 监听请求回调: `onreadystatechange`
  5. xhr.addEventListener("readystatechange", show, false);
  6. // 3. 初始化请求参数: `open(请求类型,请求地址,是否异步)`
  7. xhr.open("POST", "data/test1.php", true);
  8. // 4. 设置请求头: `setRequestHeader()`
  9. xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
  10. // 用js对象来模拟表单数据
  11. const user = {
  12. email: "admin@php.cn",
  13. password: "123456",
  14. };
  15. // 格式转换
  16. let data = JSON.stringify(user);
  17. // 5. 发送请求: `send()`
  18. xhr.send(data);
  19. // 回调函数
  20. function show(ev) {
  21. if (xhr.readyState === 4) {
  22. // 返回的数据在xhr.responseText
  23. console.log(xhr.responseText);
  24. }
  25. }
  26. </script>

data/test1.php

  1. <?php
  2. $data = key($_POST);
  3. $user = json_decode($data);
  4. echo '邮箱:'.$user->email . '密码: ' . md5($user->password);

方式二:Ajax-POST请求,直接用json

  1. <script>
  2. // 1. 创建请求对象: `new XMLHttpRequest()`
  3. const xhr = new XMLHttpRequest();
  4. // 2. 监听请求回调: `onreadystatechange`
  5. xhr.addEventListener("readystatechange", show, false);
  6. // 3. 初始化请求参数: `open(请求类型,请求地址,是否异步)`
  7. xhr.open("POST", "data/test1.php", true);
  8. // 4. 设置请求头: `setRequestHeader()`
  9. xhr.setRequestHeader("content-type", "application/json;charset=utf-8");
  10. // 用js对象来模拟表单数据
  11. const user = {
  12. email: "admin@php.cn",
  13. password: "123456",
  14. };
  15. // 格式转换
  16. let data = JSON.stringify(user);
  17. // 5. 发送请求: `send()`
  18. xhr.send(data);
  19. // 回调函数
  20. function show(ev) {
  21. if (xhr.readyState === 4) {
  22. // 返回的数据在xhr.responseText
  23. console.log(xhr.responseText);
  24. }
  25. }
  26. </script>

data/test1.php

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

方式三:Ajax-POST请求-FormData

源码:

  1. <body>
  2. <form action="" onsubmit="return false;">
  3. <label for="username">用户名:</label>
  4. <input type="text" id="username" name="username" />
  5. <label for="email">邮箱:</label>
  6. <input type="email" id="email" name="email" />
  7. <button>保存</button>
  8. </form>
  9. <script>
  10. // FormData 对象: 将表单数据进行封装后统一提交
  11. const form = document.querySelector("form");
  12. const btn = document.querySelector("button");
  13. // 1. 创建请求对象: `new XMLHttpRequest()`
  14. const xhr = new XMLHttpRequest();
  15. // 监听事件
  16. btn.addEventListener("click", callback, false);
  17. function callback(ev) {
  18. // 2. 监听请求回调: `onreadystatechange`
  19. xhr.addEventListener("readystatechange", show, false);
  20. // 3. 初始化请求参数: `open(请求类型,请求地址,是否异步)`
  21. xhr.open("POST", "data/test1.php", true);
  22. // 4. 发送请求: `send()`
  23. // new FormData(form): 当前表单数据的封装
  24. xhr.send(new FormData(form));
  25. }
  26. // 请求的回调
  27. function show() {
  28. if (xhr.readyState === 4) {
  29. // 返回的数据在xhr.responseText
  30. console.log(xhr.responseText);
  31. }
  32. }
  33. </script>
  34. </body>

data/test1.php

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