unicloud云函数url化,外部访问,云函数接收参数

'use strict';
exports.main = async (event, context) => {
//event.body为客户端上传的参数
console.log('event : ', event.body)
var res=event.body
var obg={};
var arr=res.split('&')
for (var i = 0; i < arr.length; i++) {
obg[arr[i].split('=')[0]]=arr[i].split('=')[1]
}
return obg
};

//////////////////////////////////////////////////////

或者直接固定前端请求header(建议)

前端ajax代码

method: "POST",
header: {
'Content-Type': 'application/json'
},

云函数代码

'use strict';
exports.main = async (event, context) => {
//event为客户端上传的参数
console.log('event : ', event)
console.log('event : ', event.body)
var res = event.body
var obg = {};
var code = 0

var db = uniCloud.database();
var collection = db.collection('opendb-app');
//JSON.parse(res)为传的参数
if(!JSON.parse(res).appid||JSON.parse(res).appid==''){
return {
code:0,
msg:'找不到该app版本信息'
}
}else{
var resx = await collection.where({
appid: JSON.parse(res).appid,
stable_publish: true,
}).get()

if(resx.affectedDocs==0){
return {
code:0,
msg:'找不到该app版本信息'
}

}else{
return {
code:1,
data:resx.data
}
}
}

posted on 2021-07-19 14:17  Just丶随心  阅读(2466)  评论(0编辑  收藏  举报

导航