会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133912个问题

// pages/food/food.js
var productData = require('../../utils/productData.js');
var request = require('../../utils/request.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    location: '',
    productType: [],
    listdata: [],
    num: 1,
    latitude:'',
    longitude:''


  },
 


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      productType: productData
    });
    //进入食疗方 获取定位 
    wx.getLocation({
      success:  res=>{
               console.log(res.latitude);
               console.log(res.longitude);
             
               this.setData({
                   latitude: res.latitude,
                   longitude:res.longitude
               });
               
               wx.request({
                   url: 'http://iwenwiki.com:3002/api/lbs/location',
                   data: {
                       latitude:  this.data.latitude,
                       longitude: this.data.longitude
                   },
                   success: result => {
                       console.log(result.data.result.address_component.city);
                       var cityName = result.data.result.address_component.city.slice(0,2);
                     
                       this.setData({
                         location:cityName
                       });
                       console.log(this.data.location);
                   }
               })
  
  
  
           },
           fail: err => {
               console.log(err);
           }
       });
       console.log(this.data.location);
    // console.log(productData);
   
    var data1 = {
      city: this.data.location,
      page: this.data.num
    }
    request('get', '/api/foods/list', data1, true, (res) => {
      console.log(res.data.result);
      this.setData({
        listdata: res.data.result
      })
    }, function (error) {
      console.log(error);
    }, function () {
      console.log('已经没有数据可以加载了');
    })
    // console.log(request);

    // 获取食疗方数据
    // wx.request({
    //   url: 'http://iwenwiki.com:3002/api/foods/list',
    //   data:{
    //       city:this.data.location,
    //       page:this.data.num
    //   },
    //   success:res=>{
    //       console.log(res.data.data);
    //       wx.showLoading({
    //         title: '加载中',
    //       })
    //      if(res.data.status==200){
    //         console.log(res.data.data.result);
    //         this.setData({
    //             listdata:res.data.data.result,
    //             isShow:true
    //         })
    //      }
    //   },
    //   complete:res=>{
    //     wx.hideLoading()
    //   }
    // })
  },

  // 点击分类,进入相对应页面
  productType: function (e) {
    console.log(e);
    wx.navigateTo({
      url: '../productType/productType?ID=' + e.currentTarget.dataset.mark,
    })
  },



  // 按钮--加载更多数据
  getMore: function () {
    this.data.num++;
    console.log(this.data.num);
    request('get', '/api/foods/list', data1, true, (res) => {
        console.log(res.data.result);
        this.setData({
          listdata: this.data.listdata.concat(res.data.result),
        });
        this.setData({
          isShow: true
        })
      }, (error) => {
        console.log(error);
      },
      function () {
        this.setData({
          isShow: false
        })
        console.log('已经没有数据可以加载了');
      })

    //----------------------------
    // wx.request({
    //   url: 'http://iwenwiki.com:3002/api/foods/list',
    //   data:{
    //       city:this.data.location,
    //       page:this.data.num,
    //       isShow:false
    //   },
    //   success:res=>{
    //       console.log(res.data.data);
    //       wx.showLoading({
    //         title: '加载中',
    //       })
    //      if(res.data.status==200){
    //         console.log(res.data.data.result);
    //         this.setData({
    //             listdata:this.data.listdata.concat(res.data.data.result),

    //         })
    //      }else{
    //        this.setData({
    //          isShow:false
    //        })
    //        console.log('没有数据了,加载完了');
    //      }
    //   },
    //   complete:res=>{
    //     wx.hideLoading()
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
console.log(11);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  //下拉刷新
  onPullDownRefresh: function () {
    this.setData({
      num: 1
    })
    var data1 = {
      city: this.data.location,
      page: this.data.num
    }
    request('get', '/api/foods/list', data1, true, (res) => {
      console.log(res.data.result);
      this.setData({
        listdata: res.data.result
      })
    }, function (error) {
      console.log(error);
    }, function () {
      console.log('已经没有数据可以加载了');
    })
    //----------------------------------------------
    // wx.request({
    //   url: 'http://iwenwiki.com:3002/api/foods/list',
    //   data:{
    //       city:this.data.location,
    //       page:this.data.num
    //   },
    //   success:res=>{
    //       console.log(res.data.data);
    //       wx.showLoading({
    //         title: '加载中',
    //       })
    //      if(res.data.status==200){
    //         console.log(res.data.data.result);
    //         this.setData({
    //             listdata:res.data.data.result,
    //             // isShow:true
    //         })
    //      }
    //   },
    //   complete:res=>{
    //     wx.hideLoading()
    //   }
    // })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //下拉加载更多
    this.data.num++;
    console.log('下拉到底了');
    var data1 = {
      city: this.data.location,
      page: this.data.num,
    }
    request('get', '/api/foods/list', data1, true, (res) => {
        console.log(res.data.result);
        this.setData({
          listdata: this.data.listdata.concat(res.data.result),
        })
      }, (error) => {
        console.log(error);
      },
      function () {
        console.log('已经没有数据可以加载了');
      })
    //  wx.request({

    //   url: 'http://iwenwiki.com:3002/api/foods/list',
    //   data:{
    //       city:this.data.location,
    //       page:this.data.num,
    //       isShow:false
    //   },
    //   success:res=>{
    //       console.log(res.data.data);
    //       wx.showLoading({
    //         title: '加载中',
    //       })
    //      if(res.data.status==200){
    //         console.log(res.data.data.result);
    //         this.setData({
    //             listdata:this.data.listdata.concat(res.data.data.result),

    //         })
    //      }else{
    //        this.setData({
    //          isShow:false
    //        })
    //        console.log('没有数据了,加载完了');
    //      }
    //   },
    //   complete:res=>{
    //     wx.hideLoading()
    //   }

    //  })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

老师 视频讲课时一开始进入食疗方页面是默认北京的现在改成一进入食疗方这个页面就获取当前城市  我看后台输出data数据已经改成了当前城市 当时那个请求数据列表的请求没返回任何东西也没报错

image.png


WEB前端全系列/第十三阶段:微信小程序-安心食疗(旧)/安心食疗-定位-搜索 17851楼
WEB前端全系列/第七阶段:ECMAScript6新特性模块/ES6 第一部分 17852楼
JAVA 全系列/第十阶段:权限控制与安全认证/Shiro(旧) 17853楼

from flask import Flask,Response
from datetime import datetime,timedelta
from cmsblueprint import bp

app = Flask(__name__)
app.register_blueprint(bp)
app.config['SERVER_NAME']="momo.com:5000"

@app.route('/')
def hello_world():
    return 'Hello World!'
@app.route('/createCookie/')
def createCookie():
    resp = Response('由服务器端创建cookie信息,数据是少量的,服务器端把信息返回到客户端的request Headers,并且由客户端进行cookie信息的保存')
    # resp.set_cookie("uname","momo")
    # resp.set_cookie("pwd",'123456')

    #设置Cookie的有效期【存活时间】方式1 :max_age=以秒为单位【距离现在多少秒后cookie会过期】
    # resp.set_cookie("uname", "momo",max_age=20)
    # resp.set_cookie("pwd","123")

    #设置Cookie的有效期【存活时间】方式2 : expires= datetime类型。
    #这个时间需要设置为格林尼治时间,相对北京时间来说 会自动+8小时
    # ex = datetime(year=2020,month=4,day=29,hour=12,minute=0,second=0)
    ex = datetime(year=2020,month=4,day=29,hour=4,minute=0,second=0)
    # resp.set_cookie("uname","momo",expires=ex)
    # resp.set_cookie("pwd","1234")

    #如果max_age 和expires同时设置,系统会采用max_age
    # resp.set_cookie("unlame","momo",expires=ex,max_age=20)
    # resp.set_cookie("pwd","123")
    ex2 = datetime.now()+timedelta(days=29,hours=16)
    resp.set_cookie("pwd","123")
    # resp.set_cookie("uname","momo",expires=ex2)
    resp.set_cookie("uname","lulu",expires=ex2,domain=".momo.com")
    return resp
@app.route('/deleteCookie/')
def deleteCookie():
    resp = Response('删除cookie消息')
    resp.delete_cookie('uname')
    return resp

if __name__ == '__main__':
    app.run(debug=True)
from flask import Blueprint,request


bp = Blueprint('cms',__name__,subdomain='cms')

@bp.route('/')
def index():
    uname = request.cookies.get('uname')
        return uname or "没有获取到cookie"
老师,我和视频上的代码一样,为什么;运行出来的结果却不一样,我的结果是

OG}BE97EQ`U)YB0`XCG7{@1.png

Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/Flask高级 17857楼
JAVA 全系列/第十八阶段:亿级高并发电商项目_架构/编码(旧)/电商:基于SpringSecurity实现后台登录功能 17858楼

image.png

JAVA 全系列/第六阶段:JavaWeb开发/Servlet技术详解(旧) 17859楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 17860楼
Python 全系列/第一阶段:AI驱动的Python编程/序列 17862楼
JAVA 全系列/第六阶段:JavaWeb开发/Servlet技术详解 17863楼
Python 全系列/第六阶段:数据库与AI协同技术实战/mysql介绍与环境安装 17864楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637