一、普通请求
wx.request({
url: 'http://43.143.124.247:8282/sendEmail',
method: 'POST',
data: {
user: that.data.currarr[0][that.data.mulu[0]] + that.data.currarr[1][that.data.mulu[1]] + that.data.sushe,
pwd: '3101435196@qq.com'
},
header: {
'Content-Type': 'application/x-www-form-urlencoded', // 设置请求头 Content-Type
},
success: res => {
console.log(res);
},
fail: err => {
console.log(err);
}
})
二、云开发请求
调用端
wx.cloud.callFunction({
name: 'request_spider',
data: {
code: 'sendEmail',
url: 'http://43.143.124.247:8282/sendEmail',
data: {
user: that.data.currarr[0][that.data.mulu[0]] + that.data.currarr[1][that.data.mulu[1]] + that.data.sushe,
pwd: '3101435196@qq.com'
}
}
})
.then(res => {
console.log('update-->res', res)
})
.catch(err => {
console.log('update-->err', err)
})
被调用端
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
}) // 使用当前云环境
// var request = require('request-promise');
const rp = require('request-promise-native');
const querystring = require('querystring');
const axios = require('axios')
exports.main = async (event, context) => {
// 根据发起请求携带的不同参数进行不同的操作
const code = event.code
console.log("event---------------->", event)
if (code == 'sendEmail') {
// 进行登录操作
return sendEmail(event.url, event.data);
}
};
// 登录函数
async function sendEmail(url, data) {
const formBody = querystring.stringify(data);
try {
const options = {
method: 'POST',
uri: url,
body: formBody,
// json: false, // 自动将请求体转为JSON
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(formBody) // 设置内容长度
}
};
const response = await rp(options);
return response;
} catch (error) {
console.error(error);
return error;
}
}