微信小程序 通过云函数请求http网站接口
生活随笔
收集整理的這篇文章主要介紹了
微信小程序 通过云函数请求http网站接口
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
微信小程序正式版無法調(diào)用http類型的API接口,只有htttps類型才可以通過驗(yàn)證,可以利用云函數(shù)避免這一難點(diǎn)。下面我會(huì)給出一個(gè)小案例。
原文地址:www.920vip.net/article/70
1.前端頁面-----index.js
Page({/*** 頁面的初始數(shù)據(jù)*/data: {},history() {console.log(666666666)//調(diào)用云函數(shù)wx.cloud.callFunction({// 云函數(shù)名稱name: 'HttpApi',// 傳給云函數(shù)的參數(shù)data: {},success: function(res) {console.log(res.result)//將返回的值轉(zhuǎn)為json格式var p = JSON.parse(res.result)console.log(p)console.log(p.result)// var that = this;// that.setData({// })},fail: console.error})} })1.云函數(shù)HttpApi-----index.js
使用云函數(shù)前安裝這個(gè)命令:npm install request-promise
//npm install request-promise 先安裝這個(gè)命令 var API_URL = "http://api.juheapi.com/japi/toh?key=******************&v=1.0&month=11&day=1"// 云函數(shù)入口文件 const cloud = require('wx-server-sdk')var rp = require('request-promise'); cloud.init()// 云函數(shù)入口函數(shù) 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
總結(jié)
以上是生活随笔為你收集整理的微信小程序 通过云函数请求http网站接口的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言学生学籍管理系统 链表+存储数据到
- 下一篇: 易语言---字符串操作