小程序模板網(wǎng)

微信小程序的探討--與外站進(jìn)行數(shù)據(jù)傳輸、前后端交互

發(fā)布時(shí)間:2017-12-30 09:05 所屬欄目:小程序開(kāi)發(fā)教程

微信小程序的探討--與外站進(jìn)行數(shù)據(jù)傳輸、前后端交互、頁(yè)面之間傳值

 
 
 

與外站進(jìn)行數(shù)據(jù)傳輸

    <view class="bt-ok" bindtap="okClick">確定提交</view>

Page({
//確認(rèn)提交
  okClick: function () {

  var that = this;//此處必須重定義,才能在回調(diào)函數(shù)里使用
      wx.request({   //發(fā)起url請(qǐng)求
        url: 'http://wq.ycwjwl.com/json.php',
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        data: {  //傳遞參數(shù)
          x: 'df so handsome',
          y: '2'
        },
        header: {   //請(qǐng)求方式
          'content-type': 'application/x-www-form-urlencoded'   //post must be this
           //'content-type': 'application/json'     //this for get
        },
        success: function (res) {       //訪問(wèn)成功之后的返回值     return  data、statusCode、header 
          console.log(res);
         var r = res.data.x;      //獲取json參數(shù)里的x的值
          that.setData({   //異步傳輸數(shù)據(jù)到前端
            toastHidden: false,
            toastTxt: "提交成功",
            rt: r
          });         
        }
      })
      console.log("log:"+this.data.rt);
     }
  })

前后端交互

Page({
 data: {//頁(yè)面初始數(shù)據(jù)
    orderList: [],
    total: 0, //總價(jià)格   
  },
 onLoad: function(options) {  //自帶的事件   [說(shuō)明](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/app-service/page.html)
    // Do some initialize when page load.
  },
    //自定義函數(shù)
 returnClick: function () {
    wx.navigateBack();
  },
})
 <view class="cost">總價(jià):<span>{{total}}</span></view>
 <view class="bt-return" bindtap="returnClick">返回修改</view>

<view class="orderlist" wx:for="{{orderList}}" wx:key="item.id">     //遍歷一個(gè)數(shù)組
      <span>{{item.title}}</span><span style="float:right;">{{item.cost}} * {{item.num}}</span>
    </view>

頁(yè)面之間傳值

//home
Page({
//提交訂單

  sublimitClick: function (e) {


     var agrs = JSON.stringify(this.data.orderList);//將json數(shù)組轉(zhuǎn)化為get參數(shù)
    wx.navigateTo({
      url: '../order/order?order=' + agrs
    })
  },
})
//order
Page({
// 頁(yè)面初始化 options為頁(yè)面跳轉(zhuǎn)所帶來(lái)的參數(shù)
  onLoad: function (options) {

    //object 轉(zhuǎn) array
    var order = JSON.parse(options.order);  //將get到的json字符串轉(zhuǎn)化為json數(shù)組
    var t_order = [];
    var t_total = 0;
    for (var k in order) {
      if (order[k].num > 0) {
        t_order.push(order[k]);
        t_total = t_total + order[k].cost * order[k].num; //計(jì)算總價(jià)格
      }
    }

    this.setData({  //與前端進(jìn)行交互
      orderList: t_order,
      total: t_total
    });

  },
  })


易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開(kāi)源 碼云倉(cāng)庫(kù):starfork
本文地址:http://m.u-renovate.com/wxmini/doc/course/18323.html 復(fù)制鏈接 如需定制請(qǐng)聯(lián)系易優(yōu)客服咨詢:800182392 點(diǎn)擊咨詢
QQ在線咨詢
AI智能客服 ×