微信小程序通過 wx.request發送ajax請求
1. GET
wx.request({ url: app.globalData.pubSiteUrl + 'user-information/get-information', //url method: 'GET', //請求方式 header: { 'Content-Type': 'application/json', }, data: { activityId: options.id, //參數 }, success: function(res) { if (res.data.code == 1) { _this.setData({ phone: res.data.user.phone, password: res.data.user.password }) } }, fail: function() { app.consoleLog("請求數據失敗"); }, complete: function() { // complete } })
2. POST
在小程序中,POST請求的Content-Type必須設置為:application/x-www-form-urlencoded
var _this = this; wx.request({ url: app.globalData.pubSiteUrl + 'statistics/detail-activity', //上線的話必須是https,沒有appId的本地請求貌似不受影響 method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT header: { 'Content-Type': "application/x-www-form-urlencoded", 'Cookie': 'SESSION=' + wx.getStorageSync("sessionId") }, // 設置請求的 header data: { activityId: options.id, }, success: function(res) { app.consoleLog("請求數據成功"); _this.setData({ // 設置頁面列表的內容 activityDetail: res.data.activity }); _this.getActivityDetials(); }, fail: function() { app.consoleLog("請求數據失敗"); }, complete: function() { // complete } })
