900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 微信小程序wx.request请求用POST后台得不到传递数据

微信小程序wx.request请求用POST后台得不到传递数据

时间:2019-04-05 01:53:40

相关推荐

微信小程序wx.request请求用POST后台得不到传递数据

在小程序中与后台交互数据用到的是wx.request;但是今天我用它来传递数据的时候,后台却得不到数据,

php:

header("Access-Control-Allow-Origin:*");// 响应类型header('Access-Control-Allow-Methods:POST');// 响应头设置header('Access-Control-Allow-Headers:x-requested-with, content-type');function getData($key, $default = ""){return trim(isset($_REQUEST[$key])? $_REQUEST[$key]:$default );}$tabName = getData("tabName");var_dump($tabName);

我先用ajax进行调取,可以得到:

html:

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Title</title></head><body><button>点击获取</button><p></p></body><script src="/jquery/latest/jquery.min.js"></script><script>$(() => {$("button").click(() => {$.ajax({url:"/chart/chartsData.php",type:"post",data:{"tabName":"subject"},dataType:"json",success:function (data) {console.log(data)}})})})</script></html>

可以得到我传递的参数

小程序的wxml:

wx.request({url: "/chart/chartsData.php",data: {tabName:event.currentTarget.dataset.sub},method:"POST",dataType:"json",success:function(res){console.log(res.data)}})

结果跑并没有得到,原因是在传递的时候小程序需要写上头部信息:

header: { 'content-type': 'application/x-www-form-urlencoded' },

wx.request({url: "/chart/chartsData.php",data: {tabName:event.currentTarget.dataset.sub},header: { 'content-type': 'application/x-www-form-urlencoded' },method:"POST",dataType:"json",success:function(res){console.log(res.data)}})

这样就可以啦

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。