微信发送请求示例(换取openID)


wx.login({ //wx.login 会返回code 
  success: res => {
    if (res.code) {//如果res.code包含参数则为true 执行下一步
      wx.request({//发送 res.code 到后台换取 openId, sessionKey, unionId
        url: '接口地址url', //仅为示例,并非真实的接口地址
        method: "GET",//方法get / post
        data: {
          "code": res.code
        },
        header: {//设置请求头
          'content-type': 'application/json' // 默认值
        },
        // 小程序包含多种接收方式 sueccess(成功) / fail(失败)/ complent(成功/失败) 
        success: function (res) {//请求成功接收res
          if (res.data.openid) {
            wx.setStorage({
              key: "tokenId",
              data: res.data.openid,
            })
          }
        }
      })
    }
  }
})


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM