ajax php

打印 上一主题 下一主题

主题 1668|帖子 1668|积分 5004

php安装,背景处置惩罚脚本语言。
后端开发语言不能直接允许,必须放在服务器对对应的文件夹下运行。
如:wamp的对应服务器的文件夹是www



get请求

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>02-get</title>
  6. </head>
  7. <body>
  8. <form action="02-get.php" method="get">
  9.     <input type="text" name="userName"><br>
  10.     <input type="password" name="userPwd"><br>
  11.     <input type="submit" value="提交"><br>
  12. </form>
  13. </body>
  14. </html>
复制代码
  1. <?php
  2. //print_r($_GET);//查看可以知道这回得到一个Array的数据
  3. echo $_GET["userName"];
  4. echo $_GET["userPwd"];
  5. ?>
复制代码



post

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>02-post</title>
  6. </head>
  7. <body>
  8. <form action="02-post.php" method="post">
  9.     <input type="text" name="userName"><br>
  10.     <input type="password" name="userPwd"><br>
  11.     <input type="submit" value="提交"><br>
  12. </form>
  13. </body>
  14. </html>
复制代码
  1. <?php
  2. //print_r($_GET);//查看可以知道这回得到一个Array的数据
  3. echo $_POST["userName"];
  4. echo $_POST["userPwd"];
  5. ?>
复制代码

get和post的异同点




ajax


原生步调



  • 创建异步对象

  • 设置请求方式和请求地址

  • 发送请求

  • 监听状态变革


  • 处置惩罚返回的效果
代码及征象:
  1. //4.监听状态的变化,状态变化都会执行
  2.                 xmlhttp.onreadystatechange = function () {
  3.                         }
复制代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>04-ajax-get</title>
  6.     <!--
  7.     1.什么是Ajax
  8.     -->
  9.     <script >
  10.         window.onload = function () {
  11.             var oBtn= document.querySelector("button");
  12.             oBtn.onclick = function (evl) {
  13.                 //1.需要创建异步对象
  14.                 var xmlhttp = new XMLHttpRequest();
  15.                 //2.设置请求方式和请求地址
  16.                 xmlhttp.open("GET","04-ajax-get.php",true);
  17.                 //3.发生请求
  18.                 xmlhttp.send();
  19.                 //4.监听状态的变化,状态变化都会执行
  20.                 xmlhttp.onreadystatechange = function () {
  21.                     if(xmlhttp.readyState === 4){
  22.                         //判断是否请求成功
  23.                         if(xmlhttp.status >= 200 && xmlhttp.status < 300 ||
  24.                             xmlhttp.status === 304){
  25.                             //5.处理返回的结果
  26.                             console.log("接收到服务器返回的数据");
  27.                         }
  28.                         else {
  29.                             //5.处理返回的结果
  30.                             console.log("没有接收到服务器返回的数据");
  31.                         }
  32.                     }
  33.                 }
  34.             }
  35.         }
  36.     </script>
  37. </head>
  38. <body>
  39. <button>发生请求</button>
  40. </body>
  41. </html>
复制代码
  1. <?php
  2. echo "ajax get test"
  3. ?>
复制代码

完整步调代码及效果:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>04-ajax-get</title>
  6.     <!--
  7.     1.什么是Ajax
  8.     -->
  9.     <script >
  10.         window.onload = function () {
  11.             var oBtn= document.querySelector("button");
  12.             oBtn.onclick = function (evl) {
  13.                 //1.需要创建异步对象
  14.                 var xmlhttp = new XMLHttpRequest();
  15.                 //2.设置请求方式和请求地址
  16.                 xmlhttp.open("GET","04-ajax-get.php",true);
  17.                 //3.发生请求
  18.                 xmlhttp.send();
  19.                 //4.监听状态的变化,状态变化都会执行
  20.                 xmlhttp.onreadystatechange = function () {
  21.                     if(xmlhttp.readyState === 4){
  22.                         //判断是否请求成功
  23.                         if(xmlhttp.status >= 200 && xmlhttp.status < 300 ||
  24.                             xmlhttp.status === 304){
  25.                             //5.处理返回的结果
  26.                             //console.log("接收到服务器返回的数据");
  27.                             alert(xmlhttp.responseText);
  28.                         }
  29.                         else {
  30.                             //5.处理返回的结果
  31.                             //console.log("没有接收到服务器返回的数据");
  32.                         }
  33.                     }
  34.                 }
  35.             }
  36.         }
  37.     </script>
  38. </head>
  39. <body>
  40. <button>发生请求</button>
  41. </body>
  42. </html>
复制代码
  1. <?php
  2. echo "ajax get test"
  3. ?>
复制代码

jquery步调



优点

小写也可正常发送


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

写过一篇

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表