jq基础-ajax使用

101 阅读1分钟
<table border="1">
        <tr>
            <th>订单ID</th>
            <th>用户ID</th>
            <th>订单编号</th>
            <th>订单价格</th>
            <th>是否付款</th>
            <th>发票抬头</th>
            <th>创建时间</th>
            <th>更新时间</th>
        </tr>



    </table>

    <script src="./jquery-1.12.4.js"></script>
    

        /* 用常量来表示一个基础路径 */
        const baseUrl = 'http://timemeetyou.com:8889/api/private/v1/';
        getOrderList();
        function getOrderList() {
            $.ajax({
                url: `${baseUrl}orders`,
                method: 'get',
                data: {
                    pagenum: 1,/* 必传参数 表示当前是第一页 */
                    pagesize: 10,/* 必传参数 表示一页显示10条 */
                },
                /* headers代表请求头 */
                headers: {
                    Authorization: window.localStorage.getItem('token')
                },
                success: function (res) {
                    res.data.goods.forEach(function (item) {
                        $('table').append(`
                        <tr>
                            <td>${item.order_id}</td>
                            <td>${item.user_id}</td>
                            <td>${item.order_number}</td>
                            <td>${item.order_price}</td>
                            <td>${item.order_pay}</td>
                            <td>${item.order_fapiao_title}</td>
                            <td>${rq(item.create_time)}</td>
                            <td>${rq(item.update_time)}</td>
                        </tr>
                    `)
                    });
                }
            })
        }

        /* 封装一个把毫秒数转成1970-9-8格式的日期 */
        function rq(hm) {
            let d = new Date(hm);
            let rqStr = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate();
            return rqStr
        }