亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

for循環接口請求,怎么在成功后知道當前請求成功的是哪個

for循環接口請求,怎么在成功后知道當前請求成功的是哪個

慕慕森 2018-08-10 16:24:29
在循環請求后,不支持es6語法,也不傳給服務器json格式,接口只會返回成功狀態,沒有任何的標識,在前端怎么知道當前傳遞成功的是哪個數組元素。謝謝var arr = [{  gymAddr: "00000000",  trainingActionId: "123",  deviceId: "1"}, {  gymAddr: "00000000",  trainingActionId: "123",  deviceId: "2"}, {  gymAddr: "00000000",  trainingActionId: "123",  deviceId: "3"}]for (var i = 0; i < arr.length; i++) {   wx.request({    url: "https://localhost:8080/trainingRecord/single",    data: arr[i],    method: 'POST',    header: {      'content-type': 'application/x-www-form-urlencoded'     },    success: function(res) {      console.log('res', res);      console.log(this)     }   }) }
查看完整描述

1 回答

?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

for (let i = 0; i < arr.length; i++) { // 注意這一行
  request({
    url: "https://localhost:8080/trainingRecord/single",
    data: arr[i],
    method: 'POST',
    header: {      'content-type': 'application/x-www-form-urlencoded'
    },
    success: function(res) {
      console.log('res', res, i);
      console.log(this)
    }
  })
}

或者:

for (var i = 0; i < arr.length; i++) {
 // 注意下面這一行
  (function(i){
    request({
        url: "https://localhost:8080/trainingRecord/single",
        data: arr[i],
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          console.log('res', res, i);
          console.log(this)
        }
    })
  })(i)
}


查看完整回答
反對 回復 2018-08-12
  • 1 回答
  • 0 關注
  • 1265 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號