请教如何用 wx.request值传递给下一个wx.request?

发布于 6 年前作者 zxu13956 次浏览最后编辑 6 年前来自 issues

微信小程序,我想把 wx.request出来的token值,作为参数传递给下一个wx.request用来获取文件上传的链接,我是小白,该如何写啊?
如下写第二个wx.request拿不到token,无法查找到文件上传链接。
多谢多谢,万分感谢!
gettoken: function () {
    wx.request({
      url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET’,
      header: {
        ‘content-type’: ‘application/json’ // 默认值
      },
         //success(res) {
          // console.log(res.data)
         //}
    }),
     wx.request({
      url: 'https://api.weixin.qq.com/tcb/uploadfile?access_token=res.data’,
      header: {
        ‘content-type’: ‘application/json’ // 默认值
      },
      success(res) {
        console.log(res.data)
     }
    })
  }

7 回复
mye
mye1 楼6 年前

你这样写法就异步了

chaofeng
chaofeng2 楼6 年前

gettoken: function () {


    wx.request({


      url: ‘https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET’,


      header: {


        ‘content-type’: ‘application/json’ // 默认值


      },


         success(res) {


     wx.request({


      url: ‘https://api.weixin.qq.com/tcb/uploadfile?access_token=’+res.data,


      header: {


        ‘content-type’: ‘application/json’ // 默认值


      },


      success(res) {


        console.log(res.data)


     }


    })


         }


    }),



  }

不过不建议在客户端取token,服务端

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html

mingtang
mingtang3 楼6 年前

方法1   第二个wx.request放在第一个的success里面

方法二 异步改同步  async  await

qiang48
qiang484 楼6 年前

自己用promise封装request

naliang
naliang5 楼6 年前

把wx.request用promise封装然后用async await ,不建议多重回调,可读性差.

gangren
gangren6 楼6 年前

promise callback      (setTimeout)

slai
slai7 楼5 年前

第二个request嵌套在第一个的success里面用