• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

  • 共47篇

    微信小程序

关闭

返回栏目

关闭

返回微信小程序栏目

27 - API - wx.request({}) - 发送Ajax

作者:

贺及楼

成为作者

更新日期:2022-02-05 22:41:01

作用:向服务器传数据

  1. wx.request({
  2. url: 'test.php', //仅为示例,并非真实的接口地址
  3. data: {
  4. x: '',
  5. y: ''
  6. },
  7. header: {
  8. 'content-type': 'application/json' // 默认值
  9. },
  10. success (res) {
  11. console.log(res.data)
  12. }
  13. })

GET

  1. wx.request({
  2. url: app.globalData.pubSiteUrl + 'user-information/get-information', //url
  3. method: 'GET', //请求方式
  4. header: {
  5. 'Content-Type': 'application/json',
  6. },
  7. data: {
  8. activityId: options.id, //参数
  9. },
  10. success: function(res) {
  11. if (res.data.code == 1) {
  12. _this.setData({
  13. phone: res.data.user.phone,
  14. password: res.data.user.password
  15. })
  16. }
  17. },
  18. fail: function() {
  19. app.consoleLog("请求数据失败");
  20. },
  21. complete: function() {
  22. // complete
  23. }
  24. })

POST

在小程序中,POST请求的Content-Type必须设置为:application/x-www-form-urlencoded

  1. var _this = this;
  2. wx.request({
  3. url: app.globalData.pubSiteUrl + 'statistics/detail-activity', //上线的话必须是https,没有appId的本地请求貌似不受影响
  4. method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  5. header: {
  6. 'Content-Type': "application/x-www-form-urlencoded",
  7. 'Cookie': 'SESSION=' + wx.getStorageSync("sessionId")
  8. }, // 设置请求的 header
  9. data: {
  10. activityId: options.id,
  11. },
  12. success: function(res) {
  13. app.consoleLog("请求数据成功");
  14. _this.setData({ // 设置页面列表的内容
  15. activityDetail: res.data.activity
  16. });
  17. _this.getActivityDetials();
  18. },
  19. fail: function() {
  20. app.consoleLog("请求数据失败");
  21. },
  22. complete: function() {
  23. // complete
  24. }
  25. })