美文网首页
使用flask架设新闻类微信小程序——菜鸟入门手记(4)微信小程

使用flask架设新闻类微信小程序——菜鸟入门手记(4)微信小程

作者: 林木木_f297 | 来源:发表于2019-05-07 18:51 被阅读0次

    今天肝的是微信小程序的前台www

    问题1:前台如何通过 POST 请求与我们的 flask 后台交互

    步骤:

    1.后台写一个路由

    @app.route('/getnew',methods=['GET', 'POST'])
    def getnew1():
        title = request.form.get('title')
        print(title)
        return jsonify('1')
    

    2.微信小程序在 js 的onload函数中添加一个request

    var that = this//这个不能丢
        wx.request({
          url: ‘http://127.0.0.1:5000/getnew’,
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          data: {
            title : that.data.news_title
          },
          method : 'POST',
          success: function (res) {
          }
        })
    

    踩坑日记:

    url: ‘http://127.0.0.1:5000/getnew’,
    

    1/忘写http会报错
    2/有时候后台连不上就需要把 ‘ 换成 ` 符号擦能连上

    同时在data中

     data: {
        news_title:'',
        news_content:'',
        news_image:''
        },
    

    写好相应的数据


    成功.png

    可以看到后台已经上传成功了

    问题2:微信小程序不同页面间如何传参

    步骤

    1.现在wxml内进行数据的设置

    <view class="cu-card article {{isCard?'no-card':''}}" bindtap='newsNav'
                                data-news_image='{{item.news_image}}' 
                                data-news_content='{{item.news_content}}'
                                data-news_title='{{item.news_title}}'>
    

    2.在对应的函数中编写对应地址并附上我们的请求

    newsNav: function (e) {
        var news_title = e.currentTarget.dataset.news_title;
        var news_content = e.currentTarget.dataset.news_content;
        var news_image = e.currentTarget.dataset.news_image;
        wx.navigateTo({
          url: '../../detial/detial?news_title=' + news_title + '&news_content=' + news_content + '&news_image=' + news_image,
        })
      },
    
    成功

    于是调用成功

    相关文章

      网友评论

          本文标题:使用flask架设新闻类微信小程序——菜鸟入门手记(4)微信小程

          本文链接:https://www.haomeiwen.com/subject/nbiyoqtx.html