博客列表 >Ajax的get,post请求

Ajax的get,post请求

小丑0o鱼
小丑0o鱼原创
2021年07月20日 19:31:35497浏览
  1. 1. Ajaxget,post请求
  2. jaxAsynchronous Javascript And XML(异步JavaScriptXML)在 2005年被Jesse James Garrett提出的新术语,用来描述一种使用现有技术集合的‘新’方法,包括: HTML XHTML, CSS, JavaScript, DOM, XML, XSLT, 以及最重要的XMLHttpRequest [3] 使用Ajax技术网页应用能够快速地将增量更新呈现在用户界面上,而不需要重载(刷新)整个页面,这使得程序能够更快地回应用户的操作。
  3. get
  4. 前端请求
  5. <body>
  6. <button>ajax-get</button>
  7. <p class="tips"></p>
  8. <script>
  9. // 获得元素
  10. const btn = document.querySelector('button');
  11. const tips = document.querySelector('.tips');
  12. // btn点击事件
  13. btn.onclick = function () {
  14. // 1. 创建xhr对象
  15. const xhr = new XMLHttpRequest;
  16. // 2. 建立连接
  17. xhr.open('get', 'test1.php?id=2');
  18. xhr.responseType = 'json';
  19. // 3. 处理
  20. xhr.onload = ()=>{
  21. tips.textContent = `name:${xhr.response.name},email:${xhr.response.email}`;
  22. }
  23. xhr.onerror = ()=>{alert('error')};
  24. // 4. 发送
  25. xhr.send(null);
  26. }
  27. </script>
  28. </body>
  29. 后端处理
  30. $users = [
  31. ['id'=>1,'name'=>'张三','email'=>'zs@qq.com'],
  32. ['id'=>2,'name'=>'李四','email'=>'ls@qq.com'],
  33. ['id'=>3,'name'=>'王五','email'=>'ww@qq.com'],
  34. ];
  35. $id = $_GET['id'];
  36. $key = array_search($id,array_column($users,'id'));
  37. echo json_encode($users[$key]);
  38. post
  39. 使用FormData传参
  40. FormData 接口提供了一种表示表单数据的键值对 key/value 的构造方式,并且可以轻松的将数据通过XMLHttpRequest.send() 方法发送出去,本接口和此方法都相当简单直接。
  41. 前端
  42. <body>
  43. <div class="container">
  44. <p>用户登录</p>
  45. <form action="" onsubmit="return false">
  46. <label for="email">邮箱:</label>
  47. <input type="email" name="email" id="email" placeholder="demo@email.com" />
  48. <label for="pwd">密码:</label>
  49. <input type="password" name="pwd" id="pwd" placeholder="不少于6位" />
  50. <button>提交</button>
  51. <span class="tips"></span>
  52. </form>
  53. </div>
  54. <script>
  55. // 获取元素
  56. const form = document.querySelector('.container >form');
  57. const btn = document.querySelector('.container > form button');
  58. const tips = document.querySelector('.tips');
  59. //
  60. btn.onclick = ()=>{
  61. // 通过formData获得表单数据
  62. let data = new FormData(form);
  63. // xhr
  64. // 1. 建立xhr对象
  65. const xhr = new XMLHttpRequest();
  66. // 2. 配置xhr参数
  67. xhr.open('post', 'test2.php');
  68. // 3. 处理xhr响应
  69. xhr.onload = () => {
  70. tips.textContent = xhr.response
  71. }
  72. // 4. 发送xhr请求
  73. xhr.send(data);
  74. }
  75. </script>
  76. </body>
  77. 后台
  78. $users= [
  79. ['id'=>1,'name'=>'张三','email'=>'zs@qq.com','pwd'=>'123456'],
  80. ['id'=>2,'name'=>'李四','email'=>'ls@qq.com','pwd'=>'123456'],
  81. ['id'=>3,'name'=>'王五','email'=>'ww@qq.com','pwd'=>'123456'],
  82. ];
  83. $email = $_POST['email'];
  84. $pwd = $_POST['pwd'];
  85. $res = array_filter($users, function ($user) use ($email, $pwd) {
  86. return $user['email'] === $email && $user['pwd'] === $pwd;
  87. });
  88. // 将结果做为请求响应返回到前端
  89. echo count($res) === 1 ? '验证成功' : '验证失败';
  90. 2. 选顶卡案例
  91. 点击导航标签,内容会切换
  92. css
  93. <style>
  94. * {
  95. padding: 0;
  96. margin: 0;
  97. box-sizing: border-box;
  98. }
  99. :root {
  100. font-size: 10px;
  101. }
  102. a {
  103. text-decoration: none;
  104. color: #555;
  105. }
  106. a:hover {
  107. text-decoration: underline;
  108. color: red;
  109. }
  110. li {
  111. list-style: none;
  112. line-height: 1.6em;
  113. }
  114. li:hover {
  115. cursor: default;
  116. }
  117. .container {
  118. width: 30rem;
  119. height: 30rem;
  120. margin: 30em;
  121. font-size: 1.6rem;
  122. background-color: #e6e6e6;
  123. display: flex;
  124. flex-flow: column;
  125. }
  126. .container nav ul{
  127. height: 3.6rem;
  128. display: flex;
  129. }
  130. .container nav li {
  131. flex: auto;
  132. text-align: center;
  133. line-height: 3.6rem;
  134. background-color: #fff;
  135. }
  136. .container nav li.active {
  137. background-color: #e6e6e6;
  138. }
  139. .item {
  140. padding: 3rem;
  141. display: none;
  142. }
  143. .item.active {
  144. display: block;
  145. }
  146. </style>
  147. html
  148. <div class="container">
  149. <nav>
  150. <ul class="tab">
  151. <li class="active" data-index="1">技术文章</li>
  152. <li data-index="2">网站源码</li>
  153. <li data-index="3">原生手册</li>
  154. </ul>
  155. </nav>
  156. <ul class="item active" data-index="1">
  157. <li>PHP7新特性的快速总结</li>
  158. <li>PHP7新特性的快速总结</li>
  159. <li>PHP7新特性的快速总结</li>
  160. <li>PHP7新特性的快速总结</li>
  161. <li>PHP7新特性的快速总结</li>
  162. <li>PHP7新特性的快速总结</li>
  163. </ul>
  164. <ul class="item" data-index="2">
  165. <li>帝国cms仿婚纱摄影网站</li>
  166. <li>帝国cms仿婚纱摄影网站</li>
  167. <li>帝国cms仿婚纱摄影网站</li>
  168. <li>帝国cms仿婚纱摄影网站</li>
  169. <li>帝国cms仿婚纱摄影网站</li>
  170. </ul>
  171. <ul class="item" data-index="3">
  172. <li>PHP7的内核剖析</li>
  173. <li>PHP7的内核剖析</li>
  174. <li>PHP7的内核剖析</li>
  175. <li>PHP7的内核剖析</li>
  176. <li>PHP7的内核剖析</li>
  177. </ul>
  178. </div>
  179. js
  180. // 获得元素
  181. const nav = document.querySelector('nav ul');
  182. const tabs = document.querySelectorAll('nav ul li');
  183. const items = document.querySelectorAll('ul.item');
  184. // 事件代理
  185. nav.addEventListener('click', (ev) => {
  186. let index = ev.target.dataset.index;
  187. [tabs,items].forEach((e)=>{
  188. e.forEach((li) => {
  189. // 清除active
  190. li.classList.remove('active');
  191. })
  192. })
  193. // 设置激话
  194. ev.target.classList.add('active');
  195. [...items].filter((item)=>(item.dataset.index === index))[0].classList.add('active');
  196. })
  197. 3. 换肤案例
  198. css
  199. * {
  200. padding: 0;
  201. margin: 0;
  202. box-sizing: border-box;
  203. }
  204. :root {
  205. font-size: 10px;
  206. }
  207. .container {
  208. width: 30rem;
  209. display: grid;
  210. grid-template-columns: repeat(3, 1fr);
  211. gap: 0 1rem;
  212. }
  213. .container > img {
  214. width: 100%;
  215. opacity: 0.6;
  216. border: 3px solid #fff;
  217. }
  218. .container > img:hover {
  219. width: 110%;
  220. opacity: 1;
  221. }
  222. body {
  223. background-repeat: no-repeat;
  224. background-size: cover;
  225. background-image: url('./images/1.jpg');
  226. }
  227. html
  228. <div class="container">
  229. <img src="./images/1.jpg" alt="loading">
  230. <img src="./images/2.jpg" alt="loading">
  231. <img src="./images/3.jpg" alt="loading">
  232. </div>
  233. js
  234. const container = document.querySelector('.container');
  235. const body = document.querySelector('body');
  236. // 事件代理
  237. container.onclick = (ev) => {
  238. let imgUrl = ev.target.src;
  239. body.style.backgroundImage = `url('${imgUrl}')`;
  240. };
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议