Blogger Information
Blog 29
fans 1
comment 0
visits 23141
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JavaScript:Ajax请求类型
阿心
Original
1024 people have browsed it

Ajax—-GET请求

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Ajax---GET请求</title>
  6. </head>
  7. <body>
  8. <script>
  9. //1,创建ajax对象
  10. var ajaxGet = new XMLHttpRequest();
  11. //2,监听请求
  12. // 每当 readyState 改变时,就会触发 onreadystatechange 事件。(存储函数(或函数名),每当 readyState 属性改变时,就会调用该函数。)
  13. // status : 200: "OK" 。 404: 未找到页面
  14. ajaxGet.onreadystatechange = function(){
  15. if(ajaxGet.readyState === 4 && ajaxGet.status === 200){
  16. // responseText 获得字符串形式的响应数据。
  17. console.log(ajaxGet.responseText);
  18. }
  19. };
  20. //3,初始化参数
  21. //ajax.open(请求类型,请求URL,是否异步)
  22. ajaxGet.open("GET",'test.php',true);
  23. //4,发送请求
  24. ajaxGet.send(null);
  25. </script>
  26. </body>
  27. </html>

Ajax—-POST

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Ajax---POST</title>
  6. </head>
  7. <body>
  8. <script>
  9. //1,创建ajax对象
  10. var ajaxPost = new XMLHttpRequest();
  11. // 2,监听
  12. ajaxPost.onreadystatechange=function(){
  13. if(ajaxPost.readyState === 4 && ajaxPost.status === 200){
  14. console.log(ajaxPost.responseText);
  15. }
  16. };
  17. // 3,提交参数初始化
  18. ajaxPost.open("POST","test.php",true);
  19. // 4,设置请求头,模拟表单数组类型发送
  20. // application/x-www-form-urlencoded是默认值
  21. ajaxPost.setRequestHeader('content-type','application/x-www-form-urlencoded');
  22. //准备要发送的数据
  23. var user = {
  24. account:"admin",
  25. password:123456,
  26. };
  27. //转换user为JSON字符串
  28. var userdata = JSON.stringify(user);
  29. ajaxPost.send(userdata);
  30. </script>
  31. </body>
  32. </html>

Ajax—-POST请求(2)

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Ajax---POST请求2</title>
  6. </head>
  7. <body>
  8. <script>
  9. // 1,创建对象
  10. var ajaxPost = new XMLHttpRequest();
  11. // 2,监听
  12. ajaxPost.onreadystatechange=function(){
  13. if(ajaxPost.readyState ===4 && ajaxPost.status === 200){
  14. console.log(ajaxPost.responseText);
  15. }
  16. };
  17. // 3,提交参数初始化
  18. ajaxPost.open("POST","test1.php",true);
  19. // 4,json字符串数据提交
  20. ajaxPost.setRequestHeader('content-type','application/json;charset=utf-8');
  21. var user = {
  22. account:"admin",
  23. name:"管理员",
  24. };
  25. // 5,提交
  26. var datauser = JSON.stringify(user);
  27. ajaxPost.send(datauser);
  28. </script>
  29. </body>
  30. </html>

ajax_POST__formData请求

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>ajax_POST__formData</title>
  6. </head>
  7. <body>
  8. <script>
  9. // 1,创建对象
  10. var ajaxform=new XMLHttpRequest();
  11. ajaxform.onreadystatechange = function(){
  12. if(ajaxform.readyState === 4 && ajaxform.status === 200)
  13. {
  14. console.log(ajaxform.response);
  15. }
  16. };
  17. ajaxform.open("POST","test2.php",true);
  18. // FormData 接口提供了一种表示表单数据的键值对的构造方式
  19. var data = new FormData();
  20. data.append("account","admin");
  21. data.append("password","123456");
  22. ajaxform.send(data);
  23. </script>
  24. </body>
  25. </html>

被请求的test.php文件

  1. <?php
  2. if($_GET){
  3. $user['account'] = "admin";
  4. $user['user'] = "管理员";
  5. $user['tel'] = 10010;
  6. //将数据转换为JSON
  7. echo json_encode($user);
  8. }
  9. if($_POST){
  10. // print_r($_POST);
  11. //转换为字符串
  12. $data = key($_POST);
  13. //将data转换为PHP可以处理的数据(json转为PHP对象、数组)
  14. $user = json_decode($data);
  15. print_r($user);
  16. $user = json_decode($data,true);
  17. print_r($user);
  18. echo $user['account'];
  19. echo '<hr>';
  20. }

被请求test1.php处理文件

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

总结:以上示例看上去不难。结合连接数据库连接登录视乎就看不懂了。

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