博客列表 >12月25日_AJAX请求

12月25日_AJAX请求

fkkf467
fkkf467原创
2020年01月02日 12:07:20767浏览

1. $.post() 数据提交

post.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>post数据提交</title>
  6. <script type="text/javascript" src="../jquery-3.4.1.min.js"></script>
  7. <style type="text/css">
  8. form * {
  9. margin-bottom: 2px;
  10. }
  11. form button{
  12. margin-left: 67px;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <form>
  18. <div>
  19. <label for="username">用户名:</label>
  20. <input type="text" name="username" id="username" placeholder="请输入用户名">
  21. </div>
  22. <div>
  23. <label for="pwd">密 码:</label>
  24. <input type="password" name="pwd" id="pwd" placeholder="请输入密码">
  25. </div>
  26. <button type="button" onclick="login()">登录</button>
  27. </form>
  28. <script type="text/javascript">
  29. function login(){
  30. var username = $.trim($('input[name="username"]').val());
  31. var pwd = $.trim($('input[name="pwd"]').val());
  32. $.post('login.php',{username:username,pwd:pwd},function(res){
  33. alert(res.msg);
  34. if (res.code == 0){
  35. window.location.href = 'index.html';
  36. }
  37. },'json');
  38. }
  39. </script>
  40. </body>
  41. </html>

login.php

  1. <?php
  2. $username = $_POST['username'];
  3. $pwd = $_POST['pwd'];
  4. if($username != 'admin'){
  5. $str = json_encode(['code'=>1,'msg'=>'用户名错误!!!']);
  6. exit($str);
  7. }
  8. if ($pwd != '123456'){
  9. $str = json_encode(['code'=>1,'msg'=>'密码错误!!!']);
  10. exit($str);
  11. }
  12. $str = json_encode(['code'=>0,'msg'=>'恭喜你,登录成功']);
  13. exit($str);

index.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>首页</title>
  6. </head>
  7. <body>
  8. 首页
  9. </body>
  10. </html>

2. $.get() 数据提交

  1. function login(){
  2. var username = $.trim($('input[name="username"]').val());
  3. var pwd = $.trim($('input[name="pwd"]').val());
  4. $.get('login.php?username='+username+'&pwd='+pwd,function(res){
  5. alert(res.msg);
  6. if (res.code == 0){
  7. window.location.href="index.html";
  8. }
  9. },'json');
  10. }
  1. $username = $_GET['username'];
  2. $pwd = $_GET['pwd'];
  3. if($username != 'admin'){
  4. $str = json_encode(['code'=>1,'msg'=>'用户名错误!!!']);
  5. die($str);
  6. }
  7. if ($pwd != '123456'){
  8. $str = json_encode(['code'=>1,'msg'=>'密码错误!!!']);
  9. die($str);
  10. }
  11. $str = json_encode(['code'=>0,'msg'=>'恭喜你,登录成功']);
  12. die($str);

3. $.get() 获取后台数据演示

get_city.php

  1. <?php
  2. $citys = [
  3. ['id'=>1,'txt'=>'济南'],
  4. ['id'=>2,'txt'=>'青岛'],
  5. ['id'=>3,'txt'=>'北京']
  6. ];
  7. $data = json_encode(['code'=>0,'msg'=>'success','data'=>$citys]);
  8. exit($data);

index.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>获取城市数据</title>
  6. <script type="text/javascript" src="../jquery-3.4.1.min.js"></script>
  7. </head>
  8. <body>
  9. <div>
  10. <select name="city">
  11. <option value="">请选择城市:</option>
  12. </select>
  13. <button onclick="getCity()">显示城市</button>
  14. </div>
  15. <script type="text/javascript">
  16. function getCity(){
  17. $.get('get_city.php',function(res){
  18. var citys = '';
  19. $.each(res.data,function(i,v){
  20. citys += '<option value="'+v.id+'">'+v.txt+'</option>';
  21. });
  22. $('select[name="city"]').append(citys);
  23. },'json');
  24. }
  25. </script>
  26. </body>
  27. </html>

4. serialize()

serialize() 方法可以表单值转为 URL 编码文本字符串
示例

  1. function login(){
  2. $.post('login.php',$('form').serialize(),function(res){
  3. console.log($('form').serialize());
  4. alert(res.msg);
  5. if (res.code == 0){
  6. window.location.href = 'index.html';
  7. }
  8. },'json');
  9. }

如果要传的参数很多,通过这种方式传,可以大大简化代码

5. 总结

学会了AJAX的常用操作。

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