微信小程序不支持http调用,但是可以通过服务器或者云函数实现,云函数相当于云服务器中的一段代码,可以使用http协议
const cloud = require(‘wx-server-sdk‘) // var request = require(‘request‘) var request = require(‘request-promise‘); cloud.init() // 云函数入口函数 exports.main = async (event, context) => { //qz return new Promise((resolve, reject) => { request({ url: event.url, method: "POST", json: true, form: { "SSID": event.id }, headers: { // "content-type": "application/json", "content-Type": "application/x-www-form-urlencoded", // ‘User-Agent‘: ‘Request-Promise‘ // "token": event.token }, }, function (error, response, body) { console.log("响应"+body) resolve(body) if (!error && response.statusCode == 200) { try { } catch (e) { reject() } } }) }) }
上面是云函数端代码,传入参数名只能是form
wx.cloud.callFunction({ name: ‘add‘, data: { url: "http://xxxxxxxxxxxxxxxx", id: "4C3FD301B77B" }, success: function (res) { console.log(res) // 3 console.log("输出结果"+res.result.success) // 3 }, complete: res => { }, })
客户端调用