为什么80%的码农都做不了架构师?   小程序

                                                                                       

微信小程序开发中需要注意:小程序只能用标准的https请求,get请求中没问题,但POST请求就不好使了。

原因:小程序请求头'Content-Type': 'application/json'  不同于平常的"application/x-www-form-urlencoded" 解决办法:

1.get请求无所谓,post请求需要封装成bean,用@Requestbody 接收

另:更改@Requestmapping的consumer

@Controller@RequestMapping(value = "/pets", method = RequestMethod.POST, consumes="application/json")public void addPet(@RequestBody Pet pet, Model model) {        // implementation omitted}

据说也可以,@Requestmapping详解看这里https://my.oschina.net/luanwu/blog/1579775

2.将默认的'Content-Type': 'application/json'  需要改成: "Content-Type": "application/x-www-form-urlencoded"

data: { cityname: "上海", key: "1430ec127e097e1113259c5e1be1ba70" }写成json格式这样也是请求不到数据的.需要转格式.

用别人的代码

span style="font-size:24px;"//index.js  //获取应用实例  var app = getApp()  Page( {    data: {      toastHidden: true,      city_name: '',    },    onLoad: function() {      that = this;      wx.request( {        url: "http://op.juhe.cn/onebox/weather/query",        header: {          "Content-Type": "application/x-www-form-urlencoded"        },        method: "POST",       //data: { cityname: "上海", key: "1430ec127e097e1113259c5e1be1ba70" },        data: Util.json2Form( { cityname: "上海", key: "1430ec127e097e1113259c5e1be1ba70" }),        complete: function( res ) {          that.setData( {            toastHidden: false,            toastText: res.data.reason,            city_name: res.data.result.data.realtime.city_name,            date: res.data.result.data.realtime.date,            info: res.data.result.data.realtime.weather.info,          });          if( res == null || res.data == null ) {            console.error( '网络请求失败' );            return;          }        }      })    },    onToastChanged: function() {      that.setData( { toastHidden: true });    }  })  var that;  var Util = require( '../../utils/util.js' );/span   span style="font-size:24px;"!--index.wxml--  view class="container"     toast hidden="{{toastHidden}}" bindchange="onToastChanged"          {{toastText}}      /toast      view{{city_name}}/view      view{{date}}/view      view{{info}}/view  /view/span   span style="font-size:24px;"//util.js  function json2Form(json) {      var str = [];      for(var p in json){          str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));      }      return str.join("&");  }  module.exports = {    json2Form:json2Form,  }/span  

 

TAG标签: 微信小程序微信收不到信息

http://www.xiaochengxub.com/read-105451.html