zoukankan      html  css  js  c++  java
  • 微信小程序开发之大坑记之post请求

    原文:http://blog.csdn.net/walkingmanc/article/details/54237961

    微信小程序开发过程中,如果你完全按照官方文档来,那么恭喜你,90%的可能性你会掉入大坑而且还难以爬起来,有时候明显没有任何问题但是结果就是不对。

    今天就来给大家说一下客户端发起post请求时的一个坑。

    按照官方文档,我们客户端发起post请求的代码如下:

      t:function() {

      wx.request({

        url: 'https://www.meimichao.com/bee/uploadHandler',

        data: {"city":"123"

       },

        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT

       //header: {"Content-Type":"application/x-www-form-urlencoded"}, // 设置请求的 header

        success: function(res){

        console.log(JSON.stringify(res));

        },

        fail: function(res) {

         console.log(JSON.stringify(res));

        },

        complete: function() {

          // complete

        }

       })

      },

    但是你这么写,无论如何在服务器端都是获取不到参数city的值的,始终会报null。这时你只要将post请求改成get请求类型,马上就可以获取到city参数的值了,

    客户端改成:

      t:function() {

      wx.request({

        url: 'https://www.meimichao.com/bee/uploadHandler',

        data: {"city":"123"

       },

        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT

       //header: {"Content-Type":"application/x-www-form-urlencoded"}, // 设置请求的 header

        success: function(res){

        console.log(JSON.stringify(res));

        },

        fail: function(res) {

         console.log(JSON.stringify(res));

        },

        complete: function() {

          // complete

        }

       })

      },

    服务器端:

     

    非常奇怪的是,我们只要在post请求中加上下面这个请求头,服务器端马上就可以获取到city参数的值了。

    header: {"Content-Type":"application/x-www-form-urlencoded"},

    对此官方文档是没有任何说明的,只有在你跌入坑了以后你才知道。

  • 相关阅读:
    Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
    Appcan——Box
    基于SSH框架的网上商城的质量属性
    框架的选择
    软件体系结构的认识
    Cocos2d-x项目创建方式
    一、设计模式简介
    数组与字符串四(例子、使用(2))
    数组与字符串三(Cocos2d-x 3.x _Array容器)
    数组与字符串二(例子、使用)
  • 原文地址:https://www.cnblogs.com/shihaiming/p/6265434.html
Copyright © 2011-2022 走看看