zoukankan      html  css  js  c++  java
  • 微信小程序发送ajax

    微信小程序通过 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 
      }
    })
  • 相关阅读:
    PHP文件系统处理(二)
    PHP中的文件系统处理(一)
    PHP中常用正则表达式大全
    PHP中的正则表达式的使用
    SLF4J日志框架
    内部类
    计算机存储单位
    Maven 要点
    Maven 父类工程创建及引用
    Eclipse Maven Web项目创建
  • 原文地址:https://www.cnblogs.com/xiaolinstudy/p/9510299.html
Copyright © 2011-2022 走看看