小程序端:
wx.request({
url: this.globalData.url + url,
data: data,
method: "POST",
header: 'Content-Type: application/json',
complete: res => {
callback(res)
}
})
后台接收数据:
//后端必须允许前端定义Content-Type之类的头请求
header('Access-Control-Allow-Headers:x-requested-with,content-type');
header('content-type:application/json');
date_default_timezone_set("Asia/Shanghai");
//必须使用$GLOBALS['HTTP_RAW_POST_DATA']或者file_get_contents('php://input')取数据
$rws_post = $GLOBALS['HTTP_RAW_POST_DATA'];
$post_data = json_decode($rws_post); // 转成对象
//var_dump($post_data);
//提前表单字段值
$data['uname'] = $post_data->uname;
新蔷程序员特训营,带领初学者快速开发商业项目:https://paishenwx.taobao.com/