微信小程序 通过云函数请求http网站接口
生活随笔
收集整理的這篇文章主要介紹了
微信小程序 通过云函数请求http网站接口
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
微信小程序正式版無法調用http類型的API接口,只有htttps類型才可以通過驗證,可以利用云函數避免這一難點。下面我會給出一個小案例。
原文地址:www.920vip.net/article/70
1.前端頁面-----index.js
Page({/*** 頁面的初始數據*/data: {},history() {console.log(666666666)//調用云函數wx.cloud.callFunction({// 云函數名稱name: 'HttpApi',// 傳給云函數的參數data: {},success: function(res) {console.log(res.result)//將返回的值轉為json格式var p = JSON.parse(res.result)console.log(p)console.log(p.result)// var that = this;// that.setData({// })},fail: console.error})} })1.云函數HttpApi-----index.js
使用云函數前安裝這個命令:npm install request-promise
//npm install request-promise 先安裝這個命令 var API_URL = "http://api.juheapi.com/japi/toh?key=******************&v=1.0&month=11&day=1"// 云函數入口文件 const cloud = require('wx-server-sdk')var rp = require('request-promise'); cloud.init()// 云函數入口函數 exports.main = async (event, context) => {const wxContext = cloud.getWXContext()let url = API_URL;return await rp(url).then(function (res) {return res}).catch(function (err) {return '失敗'}); } //==扣扣群聊172842597==歡迎加入論壇
官方地址:https://www.920vip.net/
官方QQ群交流: 779133600
總結
以上是生活随笔為你收集整理的微信小程序 通过云函数请求http网站接口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言学生学籍管理系统 链表+存储数据到
- 下一篇: 易语言---字符串操作