我的账户
猩码学苑

专注C++开发菁英教育

亲爱的游客,欢迎!

已有账号,请

如尚未注册?

蒋鹏冲-20221130-Ajax02

[复制链接]
北纬33 发表于 2022-11-30 23:42:38 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
一,今日学习内容:
1.// ajax封装
function ajaxPs() {
    let query = 'id=' + ipt.value;
    // 发送ajax请求
    var xhr = new XMLHttpRequest();

    xhr.open('post', 'http://121.89.216.9/show');
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
    xhr.send(query);

    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            // console.log(JSON.parse(xhr.responseText));
            let res = JSON.parse(xhr.responseText).data;
            // 渲染
            rendList(res);
        }
    }
}
function ajaxPl() {
    // 发送ajax请求
    var xhr = new XMLHttpRequest();

    xhr.open('post', 'http://121.89.216.9/list');
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
    xhr.send();

    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            // console.log(JSON.parse(xhr.responseText));
            let res = JSON.parse(xhr.responseText).data;
            // 渲染
            rendList1(res);
        }
    }
}

// 渲染
function rendList(res) {
    let html = `
            <tr>
                <td>${res.id}</td>
                <td>${res.name}</td>
                <td>${res.age}</td>
                <td>${res.sex}</td>
                <td>${res.tel}</td>
            </tr>`
    document.querySelector('table').innerHTML = html;
}

function rendList1(stuList) {
    let html = '';
    stuList.forEach(function (item, index) {
        // console.log(item);
        html += `<tr data-index='${item.id}' class='tr'>
                    <td>${item.id}</td>
                    <td>${item.name}</td>

                    <td>${item.age}</td>
                    <td>${item.sex}</td>
                    <td>${item.tel}</td>
                    <td><button class="edit">编辑</button><button class="del">删除</button></td>
                </tr>`
        // <td>${item.sno}</td>
    });
    // console.log(html);
    tbody.innerHTML = html;
}


回复

使用道具 举报

关注0

粉丝0

帖子91

发布主题
大家都在学
课堂讨论
一周热帖排行最近7x24小时热帖
关注我们
专注C++菁英教育

客服电话:18009298968

客服时间:9:00-21:00

猩码学苑 - 专注C++开发菁英教育!( 陕ICP备2025058934号-1 )

版权所有 © 陕西菁英数字科技有限公司 2023-2026