小程序请求(get+post)

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

1,默认get请求

wx.request({

url: 'test.php', //仅为示例,并非真实的接口地址

data: { x: '' , y: '' },

header: {

'content-type': 'application/json' // 默认值

},success: function(res) {

console.log(res.data)

}

})

2,post请求

wx.request( {

      url: "  ",

      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;

        }

      }

    })

猜你喜欢

转载自blog.csdn.net/qq_30299243/article/details/84137278
今日推荐