请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

老师,onload的时候不发送index请求是怎么回事?而且代码好像获取不到banner_list,cat_list

//index.js
//获取应用实例
var app = getApp();
Page({
    data: {
        indicatorDots: true,
        autoplay: true,
        interval: 3000,
        duration: 1000,
        loadingHidden: false, // loading
        swiperCurrent: 0,
        categories: [],
        activeCategoryId: 0,
        goods: [],
        scrollTop: "0",
        loadingMoreHidden: true,
        searchInput: '',
    },
    onLoad: function () {
        var that = this;

        wx.setNavigationBarTitle({
            title: app.globalData.shopName
        });

        that.setData({
            banners: [
                {
                    "id": 1,
                    "pic_url": "/images/food.jpg"
                },
                {
                    "id": 2,
                    "pic_url": "/images/food.jpg"
                },
                {
                    "id": 3,
                    "pic_url": "/images/food.jpg"
                }
            ],
            categories: [
                {id: 0, name: "全部"},
                {id: 1, name: "川菜"},
                {id: 2, name: "东北菜"},
            ],
            activeCategoryId: 0,
            goods: [
                {
                    "id": 1,
                    "name": "小鸡炖蘑菇-1",
                    "min_price": "15.00",
                    "price": "15.00",
                    "pic_url": "/images/food.jpg"
                },
                {
                    "id": 2,
                    "name": "小鸡炖蘑菇-1",
                    "min_price": "15.00",
                    "price": "15.00",
                    "pic_url": "/images/food.jpg"
                },
                {
                    "id": 3,
                    "name": "小鸡炖蘑菇-1",
                    "min_price": "15.00",
                    "price": "15.00",
                    "pic_url": "/images/food.jpg"
                },
                {
                    "id": 4,
                    "name": "小鸡炖蘑菇-1",
                    "min_price": "15.00",
                    "price": "15.00",
                    "pic_url": "/images/food.jpg"
                }

            ],
            loadingMoreHidden: false
        });

        this.getBannerAndCat();
    },
    scroll: function (e) {
        var that = this, scrollTop = that.data.scrollTop;
        that.setData({
            scrollTop: e.detail.scrollTop
        });
    },
    //事件处理函数
    swiperchange: function (e) {
        this.setData({
            swiperCurrent: e.detail.current
        })
    },
    listenerSearchInput: function (e) {
        this.setData({
            searchInput: e.detail.value
        });
    },
    toSearch: function (e) {
        this.setData({
            p: 1,
            goods: [],
            loadingMoreHidden: true
        });
        this.getFoodList();
    },
    tapBanner: function (e) {
        if (e.currentTarget.dataset.id != 0) {
            wx.navigateTo({
                url: "/pages/food/info?id=" + e.currentTarget.dataset.id
            });
        }
    },
    toDetailsTap: function (e) {
        wx.navigateTo({
            url: "/pages/food/info?id=" + e.currentTarget.dataset.id
        });
    },
    getBannerAndCat: function () {
        var that = this;
        wx.request({
            url: app.buildUrl("/food/index"),
            header: app.getRequestHeader(),
            success: function (res) {
                var resp = res.data;
                if (resp.code != 200) {
                    app.alert({"content": resp.msg});
                    return;
                }
                that.setData({
                    banners: resp.data.banner_list,
                    categories: resp.data.cat_list
                });
            }
        })
    }
});

而且在that.setData({
banners: resp.data.banner_list,
categories: resp.data.cat_list
});
代码好像获取不到banner_list,cat_list
下面是api中的food.py里面的代码

@route_api.route("/food/index")
def foodIndex():
    resp = {'code': 200, 'msg': '操作成功~', 'data': {}}
    cat_list = FoodCat.query.filter_by(status=1).order_by(FoodCat.weight.desc()).all()
    data_cat_list = []
    data_cat_list.append({
        'id': 0,
        'name': "全部"
    })
    if cat_list:
        for item in cat_list:
            tmp_data = {
                'id': item.id,
                'name': item.name
            }
            data_cat_list.append(tmp_data)
    resp['data']['cat_list'] = data_cat_list

    food_list = Food.query.filter_by(status=1) \
        .order_by(Food.total_count.desc(), Food.id.desc()).limit(3).all()

    data_food_list = []
    if food_list:
        for item in food_list:
            tmp_data = {
                'id': item.id,
                'pic_url': UrlManager.buildImageUrl(item.main_image)
            }
            data_food_list.append(tmp_data)

    resp['data']['banner_list'] = data_food_list
    return jsonify(resp)

正在回答

5回答

你好

你用小程序的开发者工具看看网络请求 到底有没有发送出去。

0 回复 有任何疑惑可以回复我~
  • 提问者 巴赫 #1
    非常感谢!
    回复 有任何疑惑可以回复我~ 2020-04-21 21:21:05
  • 提问者 巴赫 #2
    老师只有一个  check-reg 请求
    回复 有任何疑惑可以回复我~ 2020-04-21 21:21:34
编程浪子 2020-04-21 23:22:18

你好

如下图

https://img1.sycdn.imooc.com/szimg/5e9f0fa809bd319511380748.jpg

0 回复 有任何疑惑可以回复我~
提问者 巴赫 2020-04-21 23:18:22

https://img1.sycdn.imooc.com//szimg/5e9f0ea009f0c33311770401.jpg

https://img1.sycdn.imooc.com/szimg/5e9f0ea0096e06d008600689.jpg

我没找到buildURL的错误在哪

0 回复 有任何疑惑可以回复我~
编程浪子 2020-04-21 23:11:40

你好

报错了,看看报错是什么

https://img1.sycdn.imooc.com/szimg/5e9f0d2a09d4a17e23161494.jpg

0 回复 有任何疑惑可以回复我~
  • 提问者 巴赫 #1
    buildUrl有问题
    回复 有任何疑惑可以回复我~ 2020-04-21 23:17:35
提问者 巴赫 2020-04-21 21:22:45

https://img1.sycdn.imooc.com/szimg/5e9ef3970976d2aa12770867.jpg就这个

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信