微信小程序wx.request踩坑记录

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bizu005/article/details/79013686

一:header['content-type'] 为 application/json 的数据

前端
    wx.request({
      url: apiUrl,  //替换成自己的服务器地址
      data: {
        aaa:1111
      },
      method:'POST|GET',
      header: { 'content-type': 'application/json'},
      success:function(res){
        callback(res);
      },
      fail:function(){
        console.log('接口调用失败')
      }
    })
POST请求方式
$a=file_get_contents('php://input');
echo $a;    //{aaa:1111}
GET请求方式
echo json_encode($_GET);    //{aaa:1111}

二:header['content-type'] 为 application/x-www-form-urlencoded 的数据

    var param={aa:11,bb:22}
    var str = []
    for (var p in param) {
      str.push(encodeURIComponent(p) + "=" + encodeURIComponent(param[p]))
    }
    str=str.join("&")
    console.log(str)    //aa=11&bb=22
    var apiUrl ='http://127.0.0.1'
    wx.request({
      url: apiUrl,  //替换成自己的服务器地址
      data: str|param,    //POST用str,GET用param
      method:'POST|GET',
      header: { 'content-type': 'application/x-www-form-urlencoded'},
      success:function(res){
        callback(res);
      },
      fail:function(){
        console.log('接口调用失败')
      }
    })


PHP服务端
POST获取
echo json_encode($_POST);           //{aa: "11", bb: "22"}
GET获取
echo json_encode($_GET);           //{aa: "11", bb: "22"}


猜你喜欢

转载自blog.csdn.net/bizu005/article/details/79013686