鱼C论坛

 找回密码
 立即注册
查看: 548|回复: 3

[已解决]jqueryAjax方法访问JSON文件返回数据

[复制链接]
发表于 2024-12-18 16:08:11 | 显示全部楼层 |阅读模式
1鱼币

jQuery在H-bulider上完整代码能运行

利用Ajax方法访问JSON文件同时返回数据;
(1)、可以利用Ajax访问JSON文件并获得数据;
(2)、将获得数据添加到表格并对表格的样式进行合理的设置;
(3)、可以实现“修改”功能的;
(4)、可以实现“删除”功能的
最佳答案
2024-12-18 16:08:12
赵屿 发表于 2024-12-18 17:07
将以下代码用ajax方法传参



       
               
               
                jQuer ...

  1. <!DOCTYPE html>
  2. <html>

  3. <head>
  4.     <meta charset="utf-8">
  5.     <title>jQuery UI Dialog - Modal form</title>
  6.     <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  7.     <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
  8.     <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  9.     <style>
  10.         label,
  11.         input {
  12.             display: block;
  13.         }

  14.         input.text {
  15.             margin-bottom: 12px;
  16.             width: 95%;
  17.             padding: .4em;
  18.         }

  19.         fieldset {
  20.             padding: 0;
  21.             border: 0;
  22.             margin-top: 25px;
  23.         }

  24.         h1 {
  25.             font-size: 1.2em;
  26.             margin: .6em 0;
  27.         }

  28.         div#users-contain {
  29.             width: 350px;
  30.             margin: 20px 0;
  31.         }

  32.         div#users-contain table {
  33.             margin: 1em 0;
  34.             border-collapse: collapse;
  35.             width: 100%;
  36.         }

  37.         div#users-contain table td,
  38.         div#users-contain table th {
  39.             border: 1px solid #eee;
  40.             padding: .6em 10px;
  41.             text-align: left;
  42.         }

  43.         .ui-dialog .ui-state-error {
  44.             padding: .3em;
  45.         }

  46.         .validateTips {
  47.             border: 1px solid transparent;
  48.             padding: 0.3em;
  49.         }
  50.     </style>
  51. </head>

  52. <body>
  53.     <div id="dialog-form" title="添加新用户">
  54.         <p class="validateTips">All form fields are required.</p>

  55.         <form>
  56.             <fieldset>
  57.                 <label for="name">姓名</label>
  58.                 <input type="text" name="name" id="name" class="text ui-widget-content ui-corner-all">
  59.                 <label for="email">邮箱</label>
  60.                 <input type="text" name="email" id="email" class="text ui-widget-content ui-corner-all">
  61.                 <label for="password">密码</label>
  62.                 <input type="password" name="password" id="password" class="text ui-widget-content ui-corner-all">
  63.             </fieldset>
  64.         </form>
  65.     </div>

  66.     <div id="dialog-form1" title="修改用户"></div>
  67.     <div id="dialog-form2" title="确定删除吗?"></div>

  68.     <div id="users-contain" class="ui-widget">
  69.         <h1>Existing Users:</h1>
  70.         <table id="users" class="ui-widget ui-widget-content">
  71.             <thead>
  72.                 <tr class="ui-widget-header">
  73.                     <th>姓名</th>
  74.                     <th>邮箱</th>
  75.                     <th>密码</th>
  76.                     <th>操作</th>
  77.                 </tr>
  78.             </thead>
  79.             <tbody>
  80.                 <tr>
  81.                     <td>John Doe</td>
  82.                     <td>john.doe@example.com</td>
  83.                     <td>johndoe1</td>
  84.                     <td>
  85.                         <button class="edit">修改</button>
  86.                         <button class="del">删除</button>
  87.                     </td>
  88.                 </tr>
  89.             </tbody>
  90.         </table>
  91.     </div>
  92.     <button id="create-user">添加新用户</button>
  93.     <button id="bt">Load Data</button>

  94.     <script>
  95.         $(function() {
  96.             var dialog, form, name, email, password, dialog1, form1, dialog2, form2, tips = $(".validateTips");

  97.             function updateTips(t) {
  98.                 tips.text(t).addClass("ui-state-highlight");
  99.                 setTimeout(function() {
  100.                     tips.removeClass("ui-state-highlight", 1500);
  101.                 }, 500);
  102.             }

  103.             function addUser() {
  104.                 var valid = true;

  105.                 if (valid) {
  106.                     var userData = {
  107.                         name: $("#name").val(),
  108.                         email: $("#email").val(),
  109.                         password: $("#password").val()
  110.                     };
  111.                     // 使用 Ajax 发送用户数据
  112.                     $.ajax({
  113.                         type: 'POST',
  114.                         url: 'add_user.php', // 替换为您的后端处理URL
  115.                         data: userData,
  116.                         success: function(response) {
  117.                             $('#users tbody').append("<tr>" +
  118.                                 "<td>" + userData.name + "</td>" +
  119.                                 "<td>" + userData.email + "</td>" +
  120.                                 "<td>" + userData.password + "</td>" +
  121.                                 "<td><button class='edit'>修改</button> <button class='del'>删除</button></td>" +
  122.                                 "</tr>");
  123.                             dialog.dialog("close");
  124.                         }
  125.                     });

  126.                 }
  127.                 return valid;
  128.             }

  129.             dialog = $("#dialog-form").dialog({
  130.                 autoOpen: false,
  131.                 height: 400,
  132.                 width: 350,
  133.                 modal: true,
  134.                 buttons: {
  135.                     "添加": addUser,
  136.                     "取消": function() {
  137.                         dialog.dialog("close");
  138.                     }
  139.                 },
  140.                 close: function() {
  141.                     form[0].reset();
  142.                 }
  143.             });

  144.             form = dialog.find("form").on("submit", function(event) {
  145.                 event.preventDefault();
  146.                 addUser();
  147.             });

  148.             $("#create-user").button().on("click", function() {
  149.                 dialog.dialog("open");
  150.             });

  151.             $('body').on("click", '.edit', function() {
  152.                 dialog1.dialog("open");
  153.             });

  154.             $('body').on("click", '.del', function() {
  155.                 $(this).closest('tr').remove();
  156.             });

  157.             $('#bt').click(function() {
  158.                 $.ajax({
  159.                     type: 'GET',
  160.                     url: 'data.json',
  161.                     data: { 'username': 'lisi' },
  162.                     success: function(data) {
  163.                         console.log(data);
  164.                     },
  165.                     error: function() {
  166.                         console.error('错误');
  167.                     }
  168.                 });
  169.             });
  170.         });
  171.     </script>
  172. </body>

  173. </html>
复制代码


这段代码演示了如何将表单数据通过Ajax方法传递给服务器,并在表格中动态更新用户信息。在`addUser`函数中,使用`$.ajax`发送POST请求,将用户信息(姓名、邮箱、密码)发送到服务器。数据提交后,成功回调内将新用户添加到HTML表格中。请确保后端正确处理该请求(在示例中为`add_user.php`)。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

最佳答案

查看完整内容

这段代码演示了如何将表单数据通过Ajax方法传递给服务器,并在表格中动态更新用户信息。在`addUser`函数中,使用`$.ajax`发送POST请求,将用户信息(姓名、邮箱、密码)发送到服务器。数据提交后,成功回调内将新用户添加到HTML表格中。请确保后端正确处理该请求(在示例中为`add_user.php`)。 以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-12-18 16:08:12 | 显示全部楼层    本楼为最佳答案   
赵屿 发表于 2024-12-18 17:07
将以下代码用ajax方法传参



       
               
               
                jQuer ...

  1. <!DOCTYPE html>
  2. <html>

  3. <head>
  4.     <meta charset="utf-8">
  5.     <title>jQuery UI Dialog - Modal form</title>
  6.     <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  7.     <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
  8.     <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  9.     <style>
  10.         label,
  11.         input {
  12.             display: block;
  13.         }

  14.         input.text {
  15.             margin-bottom: 12px;
  16.             width: 95%;
  17.             padding: .4em;
  18.         }

  19.         fieldset {
  20.             padding: 0;
  21.             border: 0;
  22.             margin-top: 25px;
  23.         }

  24.         h1 {
  25.             font-size: 1.2em;
  26.             margin: .6em 0;
  27.         }

  28.         div#users-contain {
  29.             width: 350px;
  30.             margin: 20px 0;
  31.         }

  32.         div#users-contain table {
  33.             margin: 1em 0;
  34.             border-collapse: collapse;
  35.             width: 100%;
  36.         }

  37.         div#users-contain table td,
  38.         div#users-contain table th {
  39.             border: 1px solid #eee;
  40.             padding: .6em 10px;
  41.             text-align: left;
  42.         }

  43.         .ui-dialog .ui-state-error {
  44.             padding: .3em;
  45.         }

  46.         .validateTips {
  47.             border: 1px solid transparent;
  48.             padding: 0.3em;
  49.         }
  50.     </style>
  51. </head>

  52. <body>
  53.     <div id="dialog-form" title="添加新用户">
  54.         <p class="validateTips">All form fields are required.</p>

  55.         <form>
  56.             <fieldset>
  57.                 <label for="name">姓名</label>
  58.                 <input type="text" name="name" id="name" class="text ui-widget-content ui-corner-all">
  59.                 <label for="email">邮箱</label>
  60.                 <input type="text" name="email" id="email" class="text ui-widget-content ui-corner-all">
  61.                 <label for="password">密码</label>
  62.                 <input type="password" name="password" id="password" class="text ui-widget-content ui-corner-all">
  63.             </fieldset>
  64.         </form>
  65.     </div>

  66.     <div id="dialog-form1" title="修改用户"></div>
  67.     <div id="dialog-form2" title="确定删除吗?"></div>

  68.     <div id="users-contain" class="ui-widget">
  69.         <h1>Existing Users:</h1>
  70.         <table id="users" class="ui-widget ui-widget-content">
  71.             <thead>
  72.                 <tr class="ui-widget-header">
  73.                     <th>姓名</th>
  74.                     <th>邮箱</th>
  75.                     <th>密码</th>
  76.                     <th>操作</th>
  77.                 </tr>
  78.             </thead>
  79.             <tbody>
  80.                 <tr>
  81.                     <td>John Doe</td>
  82.                     <td>john.doe@example.com</td>
  83.                     <td>johndoe1</td>
  84.                     <td>
  85.                         <button class="edit">修改</button>
  86.                         <button class="del">删除</button>
  87.                     </td>
  88.                 </tr>
  89.             </tbody>
  90.         </table>
  91.     </div>
  92.     <button id="create-user">添加新用户</button>
  93.     <button id="bt">Load Data</button>

  94.     <script>
  95.         $(function() {
  96.             var dialog, form, name, email, password, dialog1, form1, dialog2, form2, tips = $(".validateTips");

  97.             function updateTips(t) {
  98.                 tips.text(t).addClass("ui-state-highlight");
  99.                 setTimeout(function() {
  100.                     tips.removeClass("ui-state-highlight", 1500);
  101.                 }, 500);
  102.             }

  103.             function addUser() {
  104.                 var valid = true;

  105.                 if (valid) {
  106.                     var userData = {
  107.                         name: $("#name").val(),
  108.                         email: $("#email").val(),
  109.                         password: $("#password").val()
  110.                     };
  111.                     // 使用 Ajax 发送用户数据
  112.                     $.ajax({
  113.                         type: 'POST',
  114.                         url: 'add_user.php', // 替换为您的后端处理URL
  115.                         data: userData,
  116.                         success: function(response) {
  117.                             $('#users tbody').append("<tr>" +
  118.                                 "<td>" + userData.name + "</td>" +
  119.                                 "<td>" + userData.email + "</td>" +
  120.                                 "<td>" + userData.password + "</td>" +
  121.                                 "<td><button class='edit'>修改</button> <button class='del'>删除</button></td>" +
  122.                                 "</tr>");
  123.                             dialog.dialog("close");
  124.                         }
  125.                     });

  126.                 }
  127.                 return valid;
  128.             }

  129.             dialog = $("#dialog-form").dialog({
  130.                 autoOpen: false,
  131.                 height: 400,
  132.                 width: 350,
  133.                 modal: true,
  134.                 buttons: {
  135.                     "添加": addUser,
  136.                     "取消": function() {
  137.                         dialog.dialog("close");
  138.                     }
  139.                 },
  140.                 close: function() {
  141.                     form[0].reset();
  142.                 }
  143.             });

  144.             form = dialog.find("form").on("submit", function(event) {
  145.                 event.preventDefault();
  146.                 addUser();
  147.             });

  148.             $("#create-user").button().on("click", function() {
  149.                 dialog.dialog("open");
  150.             });

  151.             $('body').on("click", '.edit', function() {
  152.                 dialog1.dialog("open");
  153.             });

  154.             $('body').on("click", '.del', function() {
  155.                 $(this).closest('tr').remove();
  156.             });

  157.             $('#bt').click(function() {
  158.                 $.ajax({
  159.                     type: 'GET',
  160.                     url: 'data.json',
  161.                     data: { 'username': 'lisi' },
  162.                     success: function(data) {
  163.                         console.log(data);
  164.                     },
  165.                     error: function() {
  166.                         console.error('错误');
  167.                     }
  168.                 });
  169.             });
  170.         });
  171.     </script>
  172. </body>

  173. </html>
复制代码


这段代码演示了如何将表单数据通过Ajax方法传递给服务器,并在表格中动态更新用户信息。在`addUser`函数中,使用`$.ajax`发送POST请求,将用户信息(姓名、邮箱、密码)发送到服务器。数据提交后,成功回调内将新用户添加到HTML表格中。请确保后端正确处理该请求(在示例中为`add_user.php`)。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-12-18 16:08:31 | 显示全部楼层
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Ajax JSON Example</title>
  6.     <style>
  7.         table {
  8.             width: 100%;
  9.             border-collapse: collapse;
  10.         }
  11.         th, td {
  12.             border: 1px solid #ddd;
  13.             padding: 8px;
  14.             text-align: left;
  15.         }
  16.         tr:nth-child(even) {
  17.             background-color: #f2f2f2;
  18.         }
  19.         tr:hover {
  20.             background-color: #ddd;
  21.         }
  22.         th {
  23.             background-color: #4CAF50;
  24.             color: white;
  25.         }
  26.     </style>
  27.     <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  28.     <script>
  29.         $(document).ready(function() {
  30.             $.ajax({
  31.                 url: 'data.json',
  32.                 dataType: 'json',
  33.                 success: function(data) {
  34.                     $.each(data, function(i, item) {
  35.                         var row = '<tr>';
  36.                         row += '<td>' + item.name + '</td>';
  37.                         row += '<td>' + item.age + '</td>';
  38.                         row += '<td><button class="edit">修改</button></td>';
  39.                         row += '<td><button class="delete">删除</button></td>';
  40.                         row += '</tr>';
  41.                         $('#data-table').append(row);
  42.                     });

  43.                     $('.edit').click(function() {
  44.                         var row = $(this).closest('tr');
  45.                         var name = prompt('请输入新的名字:', row.find('td:eq(0)').text());
  46.                         if (name) {
  47.                             row.find('td:eq(0)').text(name);
  48.                         }
  49.                     });

  50.                     $('.delete').click(function() {
  51.                         $(this).closest('tr').remove();
  52.                     });
  53.                 }
  54.             });
  55.         });
  56.     </script>
  57. </head>
  58. <body>
  59.     <h1>JSON数据表格</h1>
  60.     <table id="data-table">
  61.         <tr>
  62.             <th>名称</th>
  63.             <th>年龄</th>
  64.             <th>修改</th>
  65.             <th>删除</th>
  66.         </tr>
  67.     </table>
  68. </body>
  69. </html>
复制代码


此示例代码利用了jQuery的Ajax功能来请求名为`data.json`的JSON文件,并将数据动态添加到HTML表格中。代码还包括“修改”和“删除”功能,您可以根据提示修改表格中的内容或删除表格行。请确保`data.json`文件在同一目录下,并格式正确。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-12-18 17:07:42 | 显示全部楼层
FishC 发表于 2024-12-18 16:08
此示例代码利用了jQuery的Ajax功能来请求名为`data.json`的JSON文件,并将数据动态添加到HTML表格中。 ...

将以下代码用ajax方法传参


<html lang="zh-CN">
        <head>
                <meta charset="utf-8">
                <meta name="viewport" content="width=device-width, initial-scale=1">
                <title>jQuery UI Dialog - Modal form</title>
                <link rel="stylesheet" href="https://code.jquery.com/ui/1.14.1/themes/base/jquery-ui.css">
                <link rel="stylesheet" href="/resources/demos/style.css">
                <style>
                        label,
                        input {
                                display: block;
                        }

                        input.text {
                                margin-bottom: 12px;
                                width: 95%;
                                padding: .4em;
                        }

                        fieldset {
                                padding: 0;
                                border: 0;
                                margin-top: 25px;
                        }

                        h1 {
                                font-size: 1.2em;
                                margin: .6em 0;
                        }

                        div#users-contain {
                                width: 350px;
                                margin: 20px 0;
                        }

                        div#users-contain table {
                                margin: 1em 0;
                                border-collapse: collapse;
                                width: 100%;
                        }

                        div#users-contain table td,
                        div#users-contain table th {
                                border: 1px solid #eee;
                                padding: .6em 10px;
                                text-align: left;
                        }

                        .ui-dialog .ui-state-error {
                                padding: .3em;
                        }

                        .validateTips {
                                border: 1px solid transparent;
                                padding: 0.3em;
                        }
                </style>
                <script src="https://code.jquery.com/jquery-3.7.1.js"></script>
                <script src="https://code.jquery.com/ui/1.14.1/jquery-ui.js"></script>
                <script>
                        $(function() {
                                var dialog, form, name, email, password, th


                                tips = $(".validateTips");

                                function updateTips(t) {
                                        tips
                                                .text(t)
                                                .addClass("ui-state-highlight");
                                        setTimeout(function() {
                                                tips.removeClass("ui-state-highlight", 1500);
                                        }, 500);
                                }

                                function addUser() {
                                        var valid = true;

                                        if (valid) {
                                                $("#users tbody").append("<tr>" +
                                                        "<td>" + $("#name").val() + "</td>" +
                                                        "<td>" + $("#email").val() + "</td>" +
                                                        "<td>" + $("#password").val() + "</td>" +
                                                        "<td>" +
                                                        "<button class='edit'>修改</button>" +
                                                        "<button class='del'>删除</button>" +
                                                        "</td>" +
                                                        "</tr>");
                                                dialog.dialog("close");
                                        }
                                        return valid;
                                }

                                dialog = $("#dialog-form").dialog({
                                        autoOpen: false,
                                        height: 400,
                                        width: 350,
                                        modal: true,
                                        buttons: {
                                                "添加": addUser,
                                                取消: function() {
                                                        dialog.dialog("close");
                                                }
                                        },
                                        close: function() {
                                                form[0].reset();
                                                //allFields.removeClass( "ui-state-error" );
                                        }
                                });

                                form = dialog.find("form").on("submit", function(event) {
                                        event.preventDefault();
                                        addUser();
                                });

                                $("#create-user").click(function() {
                                        dialog.dialog("open");
                                });
                                /*仿照添加新用户完成修改功能*/
                                //1、单击修改按钮弹出对话框
                                $('body').on("click", '.edit', function() {
                                        //获取修改按钮左侧的姓名、邮箱等
                                        th = $(this).parent().prevAll();
                                        name = th.eq(2).text();
                                        email = th.eq(1).text();
                                        password = th.eq(0).text();
                                        //将获取到的三个值分别赋值给 "#dialog-form1"
                                        $('#name1').val(name);
                                        $('#email1').val(email);
                                        $('#password1').val(password);
                                        dialog1.dialog("open");
                                });

                                //2、利用jQuery-ui 框架让表单弹出
                                dialog1 = $("#dialog-form1").dialog({
                                        autoOpen: false,
                                        height: 400,
                                        width: 350,
                                        modal: true,
                                        buttons: {
                                                "确认": edit,
                                                取消: function() {
                                                        dialog1.dialog("close");
                                                }
                                        },
                                        close: function() {
                                                form1[0].reset();
                                                //allFields.removeClass( "ui-state-error" );
                                        }
                                });

                                form1 = dialog1.find("form").on("submit", function(event) {
                                        event.preventDefault();
                                        edit();
                                });

                                function edit() {
                                        name = $('#name1').val();
                                        email = $('#email1').val();
                                        password = $('#password1').val();
                                        th.eq(2).text(name);
                                        th.eq(1).text(email);
                                        th.eq(0).text(password);
                                        dialog1.dialog("close");
                                }


                                //删除
                                $('body').on("click", '.del', function() {
                                        th = $(this).parent().parent('tr');

                                        dialog2.dialog("open");
                                });

                                //2、利用jQuery-ui 框架让表单弹出
                                dialog2 = $("#dialog-form2").dialog({
                                        autoOpen: false,
                                        height: 200,
                                        width: 350,
                                        modal: true,
                                        buttons: {
                                                "删除": del,
                                                取消: function() {
                                                        dialog2.dialog("close");
                                                }
                                        },
                                        close: function() {
                                                form2[0].reset();
                                                //allFields.removeClass( "ui-state-error" );
                                        }
                                });

                                form2 = dialog2.find("form").on("submit", function(event) {
                                        event.preventDefault();
                                        del();
                                });

                                function del() {

                                        th.remove()
                                        dialog2.dialog("close");
                                }

                        });
                </script>
        </head>
        <body>

                <div id="dialog-form" title="添加新用户">
                        <p class="validateTips">All form fields are required.</p>

                        <form>
                                <fieldset>
                                        <label for="name">姓名</label>
                                        <input type="text" name="name" id="name" value="" class="text ui-widget-content ui-corner-all">
                                        <label for="email">邮箱</label>
                                        <input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all">
                                        <label for="password">密码</label>
                                        <input type="password" name="password" id="password" value=""
                                                class="text ui-widget-content ui-corner-all">

                                        <!-- Allow form submission with keyboard without duplicating the dialog button -->
                                        <input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
                                </fieldset>
                        </form>
                </div>


                <div id="dialog-form1" title="修改当前用户">
                        <p class="validateTips">All form fields are required.</p>

                        <form>
                                <fieldset>
                                        <label for="name">姓名</label>
                                        <input type="text" name="name" id="name1" value="" class="text ui-widget-content ui-corner-all">
                                        <label for="email">邮箱</label>
                                        <input type="text" name="email" id="email1" value="" class="text ui-widget-content ui-corner-all">
                                        <label for="password">密码</label>
                                        <input type="password" name="password" id="password1" value=""
                                                class="text ui-widget-content ui-corner-all">

                                        <!-- Allow form submission with keyboard without duplicating the dialog button -->
                                        <input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
                                </fieldset>
                        </form>
                </div>


                <div id="dialog-form2" title="删除当前用户">
                        <!-- <p class="validateTips">All form fields are required.</p> -->

                        <form>

                                <input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
                                <!-- </fieldset> -->
                        </form>
                </div>



                <div id="users-contain" class="ui-widget">
                        <h1>Existing Users:</h1>
                        <table id="users" class="ui-widget ui-widget-content">
                                <thead>
                                        <tr class="ui-widget-header ">
                                                <th>姓名</th>
                                                <th>邮箱</th>
                                                <th>密码</th>
                                                <th>
                                                        操作
                                                </th>
                                        </tr>
                                </thead>
                                <tbody>
                                        <tr>
                                                <td>John Doe</td>
                                                <td>john.doe@example.com</td>
                                                <td>johndoe1</td>
                                                <td>
                                                        <button class='edit'>修改</button>
                                                        <button class='del'>删除</button>
                                                </td>
                                        </tr>
                                </tbody>
                        </table>
                </div>
                <button id="create-user">添加新用户</button>

                <script>
                        $('#bt').click(function() {
                                $.ajax({
                                        //传参方式
                                        type: 'get',
                                        //前端要访问的服务器地址
                                        url: 'data.json',
                                        //前端要向服务器传的数据
                                        data: {
                                                'username': 'lisi'
                                        },

                                        success: function(data) {
                                                tab = $('<table></table>')
                                                for (var i = 0; i < data.length; i++) {
                                                        li = "<tr>" +
                                                                "<td>" + data[i].userid + "</td>" +
                                                                "<td>" + data[i].username + "</td>" +
                                                                "<td>" + data[i].userage + "</td>" +
                                                                "</tr>"
                                                        tab.append(li);
                                                }
                                                $('body').append(tab);
                                                // console.log(JSON.parse(data))
                                        },
                                        error: function() {
                                                console.log('错误')
                                        }
                                })
                        })
                </script>


        </body>
</html>
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-10 00:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表