zoukankan      html  css  js  c++  java
  • 微信小程序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="http://g.ydbcdn.com/jquery/latest/jquery.min.js"></script>
    <script>
        $(() => {
            $("button").click(() => {
                $.ajax({
                    url:"http://fm.xiaofany.com/chart/chartsData.php",
                    type:"post",
                    data:{"tabName":"subject"},
                    dataType:"json",
                    success:function (data) {
                        console.log(data)
                    }
                })
            })
        })
    </script>
    </html>
    

      

    可以得到我传递的参数

    小程序的wxml:

       wx.request({
          url: "http://fm.xiaofany.com/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: "http://fm.xiaofany.com/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)
          }
        })
    

      这样就可以啦

  • 相关阅读:
    真情感动看上海新闻娱乐频道“百家心”
    Treeview Navigation Web Part for SharePoint with SmartPart v1.0
    轻松玩转Typed DataSet, Part III
    微软Avalon和WinForms的发展蓝图
    Proxy Pattern using C#
    System.Threading.Timer类的TimerCallback 委托
    SmartPart v1.0 for SharePoint [Free Web Part]
    分布式应用架构中的数据传输对象(DTO)
    定制SharePoint Portal Server 2003站点的向导
    Bridge Pattern using C#
  • 原文地址:https://www.cnblogs.com/mmykdbc/p/8404718.html
Copyright © 2011-2022 走看看