用聚合數據API快速寫出小程序

利用聚合數據API快速寫出小程序,過程簡單。json

一、申請小程序帳號小程序

二、進入開發api

三、調用API。好比「蘇州實時公交」小程序,選擇的是蘇州實時公交API。安全

蘇州實時公交API文檔:https://www.juhe.cn/docs/api/...app

909818-20170110141537572-1061339349.gif

以下,是「蘇州實時公交」小程序調用代碼:ide

var url = "https://apis.juhe.cn/szbusline/bus";
    //爲了您的密鑰安全,建議使用服務端代碼中轉請求,事例代碼可參考 https://code.juhe.cn/
    var apiKey = "yourKey";    //輸入本身的key

    Page({
      data: {
        inputValue: '',
        restation: [],
        condition: true
      },
      //獲取輸入框的值
      bindInput: function(e) {
        var that = this;
        that.setData({
          inputValue: e.detail.value
        });
      },

    //點擊搜索按鈕調用的函數
      search:function(e){
        var that = this;

        //數據加載完成以前,顯示加載中提示框
        wx.showToast({
          title: '加載中。。。',
          icon: 'loading',
          duration: 10000
        });

        //輸入框沒有輸入的判斷
        if(that.data.inputValue == ''){
            wx.hideToast();
            return;
        }

        //發起請求,注意 wx.request發起的是 HTTPS 請求
        wx.request({
          url: url + "?station=" + that.data.inputValue + "&key=" + apiKey,
          data: {},
          header: {
              'content-type': 'application/json'
          },
          success: function(res) {
            var data = res.data;
            //將數據從邏輯層發送到視圖層,同時改變對應的 this.data 的值
            that.setData({
              restation: data.result,
              condition: false
            });
            //數據加載成功後隱藏加載中彈框
            wx.hideToast();
          }
        })

      }
    })

四、完整源碼下載函數

提供「蘇州實時公交」小程序的完整源碼下載,能夠經過完整的源碼,更好的學習如何經過調用聚合API,快速實現編寫小程序。學習

下載「蘇州實時公交」小程序代碼:https://juheimg.oss-cn-hangzh...this

相關文章
相關標籤/搜索