..._js ajax后渲染li">
    查看
    • 编辑修改
    • 首页
    • UNITY
    • NODEJS
    • PYTHON
    • AI
    • GIT
    • PHP
    • GO
    • CEF3
    • JAVA
    • HTML
    • CSS
    devbox
    盐析白兔
    这个屌丝很懒,什么也没留下!
    关注作者
    热门标签
    • jquery
    • HTML
    • CSS
    • PHP
    • ASP
    • PYTHON
    • GO
    • AI
    • C
    • C++
    • C#
    • PHOTOSHOP
    • UNITY
    • iOS
    • android
    • vue
    • xml
    • 爬虫
    • SEO
    • LINUX
    • WINDOWS
    • JAVA
    • MFC
    • CEF3
    • CAD
    • NODEJS
    • GIT
    • Pyppeteer
    • article
    热门文章
    • 1OSI模型在网络通信中是怎样被应用的
    • 2【系统架构师】-案例篇(十五)SOA、微服务与数据库_soa和微服务
    • 3基于snownlp及朴素贝叶斯的情感分析——以大众点评网评论为例_snownlp情感分析基于朴素贝叶斯
    • 4git报错:refusing to merge unrelated histories
    • 5如何将Web主页性能提升十倍以上?
    • 6详解DDR3原理以及使用Xilinx MIG IP核(app 接口)实现DDR3读写测试
    • 7基于Next14+Auth5实现Github、Google、Gitee平台授权登录和邮箱密码登录_githubnextauth
    • 8Python爬虫使用selenium爬取qq群的成员信息(全自动实现自动登陆)(1)
    • 9Leetcode 1137. 第 N 个泰波那契数_给你整数 n,请返回第 n 个泰波那契数 tn 的值。(3分)题目内容:泰波那契序列 tn 定
    • 10嵌入式截屏工具-gsnap移植 arm平台
    当前位置:   article > 正文

    js通过AJAX获取后台数据渲染到页面_js ajax后渲染li

    作者:盐析白兔 | 2024-08-11 14:29:59

    赞

    踩

    js ajax后渲染li

    1.GET

    html部分

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>AJAX发送GET请求并传递参数</title>
    </head>
    <body>
      <ul id="list"></ul>
    
      <script>
        // 获取ul的id
        var listElement = document.getElementById('list');
        // 发送ajax请求获取列表数据呈现在也页面
        // ====================================
        var xhr = new XMLHttpRequest();
        // 打开网址
        xhr.open('GET','users.php');
        // 敲回车等响应
        xhr.send();
    
        // 接收请求
        xhr.onreadystatechange = function(){
          //判断请求题是不是我们想要的数据
          if(this.readyState !== 4) return;
          // 获取传过来的json数据
          var data = JSON.parse(this.responseText);
          //data ==> 数据
    
          // for循环创建动态li
          for(var i=0; i<data.length; i++ ){
            // 创建动态li标签
            var liElement = document.createElement('li');
            // 给li添加数据
            liElement.innerHTML = data[i].name;
            // 给li添加id
            liElement.id = data[i].id;
    
            // 把li追加到ul里面去
            listElement.appendChild(liElement);
    
            // 获取点击元素的数据
            liElement.addEventListener('click',function(){
              var xhr2 = new XMLHttpRequest();
              // 打开文件
              xhr2.open('GET','users.php?id='+ this.id);  //问号传参
              // 敲回车等响应
              xhr2.send();
              console.log(this.id);
              xhr2.onreadystatechange = function(){
                //判断请求题是不是我们想要的数据
                  if(this.readyState !== 4) return;
                  // 获取传过来的json数据
                  var data2 = JSON.parse(this.responseText);
                  //data ==> 数据
                  console.log(data2);
              };
            });  
          }
        }
      </script>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    php部分

    <?php
    header('Content-Type: application/json');
    /**
     * 返回的响应就是一个 JSON 内容(返回的就是数据)
     * 对于返回数据的地址一般我们称之为接口(形式上是 Web 形式)
     */
    
    // `/users.php?id=1` => id 为 1 的用户信息
    
    $data = array(
      array(
        'id' => 1,
        'name' => '张三',
        'age' => 18
      ),
      array(
        'id' => 2,
        'name' => '李四',
        'age' => 20
      ),
      array(
        'id' => 3,
        'name' => '二傻子',
        'age' => 18
      ),
      array(
        'id' => 4,
        'name' => '三愣子',
        'age' => 19
      )
    );
    if (empty($_GET['id'])) {
      // 没有 ID 获取全部
      // 因为 HTTP 中约定报文的内容就是字符串,而我们需要传递给客户端的信息是一个有结构的数据
      // 这种情况下我们一般采用 JSON 作为数据格式
      $json = json_encode($data); // => [{"id":1,"name":"张三"},{...}]
      echo $json;
    } else {
      // 传递了 ID 只获取一条
      foreach ($data as $item) {
        if ($item['id'] != $_GET['id']) continue;
        $json = json_encode($item); // => [{"id":1,"name":"张三"},{...}]
        echo $json;
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    2.POST

    html部分

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>AJAX发送POST请求</title>
      <style>
        #loading {
          display: none;
          position: fixed;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          background-color: #555;
          opacity: .5;
          text-align: center;
          line-height: 300px;
        }
    
        #loading::after {
          content: '加载中...';
          color : #fff;
        }
      </style>
    </head>
    <body>
      <div id="loading"></div>
      <table border="1">
        <tr>
          <td>用户名</td>
          <td><input type="text" id="username"></td>
        </tr>
        <tr>
          <td>密码</td>
          <td><input type="password" id="password"></td>
        </tr>
        <tr>
          <td></td>
          <td><button id="btn">登录</button></td>
        </tr>
      </table>
      <script>
    
        var btn = document.getElementById('btn')
        // 1. 获取界面上的元素 value
        var txtUsername = document.getElementById('username')
        var txtPassword = document.getElementById('password')
        var loading = document.getElementById('loading')
    
        // 注册点击事件
        btn.onclick = function() {
          // 显示正在加载中
          loading.style.display = 'block';
          // 获取文本框内的value值
          var username = txtUsername.value;
          var password = txtPassword.value;
    
          // 发送post事件
          var xhr =new XMLHttpRequest();
          // 获取浏览器
          xhr.open('POST','login.php');
    
          // post响应操作
          xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
          // xhr.send('username=' + username + '&password=' + password); 方法1;
          xhr.send(`username=${username}&password=${password}`); //方法二
    
          // 获取服务器反馈
          xhr.onreadystatechange = function(){
            // 判断接收的数据
            if(this.readyState !== 4) return;
            //获取提示文本
            alert(this.responseText);
            // 隐藏加载中
            loading.style.display = 'none';
          }
        }
      </script>
    </body>
    </html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81

    php部分

    <?php 
    // 校验用户输入的用户名和密码
    if(empty($_POST['username']) && empty($_POST['password']) ){
      exit('请输入用户名或密码');
    }
    
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    // 校验密码是否正确
    if($username === 'zhangs' && $password === '123456' ){
      exit('登录成功');
    }
    
    exit('请输入正确的用户名或密码');
    ?>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/964716
    推荐阅读
    • article【Python 亲测有效】 FileNotFoundError: [Errno 2] No such...
      【Python 亲测有效】 FileNotFoundError: [Errno 2] No such file or d...

      赞

      踩

    • article已解决 Python FileNotFoundError 的报错问题_pyintaller file...
      已解决 Python FileNotFoundError 的报错问题_pyintaller filenotfounder...

      赞

      踩

    • articledocker 安装kafka_这几款 kafka 工具,值得了解下...
      在 Kafka 集群运维过程中,我们通常会借用一些开源工具来完成 kafka 的日常运维需求和相关问题排查,接下来介绍几...

      赞

      踩

    • articleFlask or FastAPI? Python服务端初体验_fastapi和flask...
      最近由于工作需要,又去了解了一下简单的python服务搭建的相关工作,主要是为了自己开发的模型或者工具给同组的人使用。之...

      赞

      踩

    • article已解决Python FileNotFoundError 报错的正确解决办法,亲测有效...
      报错原因FileNotFoundError报错的原因主要是:文件路径错误:指定的文件路径不正确,可能是因为文件不在该路径...

      赞

      踩

    • articlePython图像处理之识别文字(一)_python 图像识别与提取图片...
      本文介绍了如何使用Python结合Tesseract进行图片文字识别,并通过7行代码实现将图片文字导出为TXT文档。虽然...

      赞

      踩

    • articlePython __init__.py...
      Python __init__.py 作用详解尼古拉苏关注12018.06.10 12:57:34字数 745阅读 45...

      赞

      踩

    • articleSQL知识点总结_遍历匹配所有,不断向下增添...
      SQL知识点总结_遍历匹配所有,不断向下增添遍历匹配所有,不断向下增添 ...

      赞

      踩

    • article怎么重新启动协议服务器,如何启用或禁用服务器网络协议 (SQL Server PowerShell)...
      SQL Server 安装程序安装了 TCP 和 Named Pipes 网络协议,但这些协议可能并未启用。可以使用以下...

      赞

      踩

    • article短时傅里叶变换(STFT)原理及Matlab代码_matlab stft...
      本文介绍了短时傅里叶变换(STFT)与傅里叶变换的区别,STFT适用于非平稳信号的时间频率分析。详细讨论了窗长对STFT...

      赞

      踩

    • articlePython创建微信机器人...
      本文介绍了如何使用Python创建微信机器人,选择了wxpy库作为开发工具,通过模拟登录微信网页版并结合图灵机器人实现自...

      赞

      踩

    • article局域网共享文件夹怎么加密?局域网共享文件夹加密方法介绍_局域网共享文件设置密码...
      本文介绍了如何在企业局域网中通过共享文件夹加密超级大师保护数据安全,该软件操作简便,支持批量加密、多用户权限控制和高强度...

      赞

      踩

    • article【鸿蒙实战开发】HarmonyOS中WebSocket连接详解_鸿蒙 websocket...
      这份鸿蒙(HarmonyOS NEXT)资料包含了鸿蒙开发必掌握的核心知识要点,内容包含了(ArkTS、ArkUI开发组...

      赞

      踩

    • articleAndroid 多屏显示分析_android displaygroup...
      双屏异显系统提供了Presentation类,可以实现在两块屏幕上同时显示不同的内容;Presentation是一个特殊...

      赞

      踩

    • articlepython web框架哪家强?Flask、Django、FastAPI对比_python web框...
      对比Flask、Django和FastAPI三款python框架,Flask更适合新手上手。_python web框架性...

      赞

      踩

    • articlePython遇到FileNotFoundError: [Errno 2] No such file ...
      解决Python遇到FileNotFoundError: [Errno 2] No such file or direc...

      赞

      踩

    • article分布式操作系统入门:可的哥(Codigger)引领新潮流...
      在这样的系统中,每个节点既可以独立执行本地计算任务,又可以通过网络相互协同工作,以分布协同的方式处理更大规模的计算任务。...

      赞

      踩

    • article用Python轻松解析和处理大数据_python大数据处理与分析...
      在本文中,我们介绍了Python在大数据处理中的应用,着重介绍了pandas和numpy这两个常用的数据解析和处理库。n...

      赞

      踩

    • article网络安全员的职业前景如何?_公司要有 网络安全员...
      文章指出网络安全行业人才需求大,薪资高,强调实战经验和业务理解的重要性。提供了一份详细的网络安全学习路径,包括入门、技术...

      赞

      踩

    • articlevue+electron打包报错_an unhandled rejection has occurr...
      vue+electron打包报错:Output:在package.json文件中添加:_an unhandled rej...

      赞

      踩

    相关标签
    • python
    • android
    • java
    • 开发语言
    • 解决Bug
    • Bug之道
    • docker 安装kafka
    • docker安装kafka
    • flask
    • fastapi
    • 前端
    • linux
    • 图片识别
    • Python
    • tesseract
    • OCR
    • 图像处理
    • sql
    • 数据库
    • 数据分析
    • 数据挖掘
    • 学习
    • 怎么重新启动协议服务器
    • 算法

    Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。

      

    闽ICP备14008679号