收藏
回答

网络超时请求失败,请问onFailed该如何能正常回调?

报错:at api request complete callback function. TypeError: onFailed is not a function

请问如何能正确回调?上面的onSuccess是正常的,下面的onFailed就不行


function requestWithTimeOut(url, params, timeout, method, onSuccess, onFailed) {

  wx.request({

    url: url,

    data: dealParams(params),

    timeout: timeout * 1000,

    method: method,

    header: header,

    success: function (res) {

      if (res.data) {

        /** start 根据需求 接口的返回状态码进行处理 */

        if (res.statusCode == 200) {

          onSuccess(res.data); 

        } 

      }

    },

    fail: function (error) {

      console.log('网络请求失败:');

      console.log(error);

      console.log('--------');

      if(onFailed){

        onFailed(error.errMsg);

      }

    },

  })

}

最后一次编辑于  2020-11-09
回答关注问题邀请回答
收藏

1 个回答

  • this
    this
    2020-11-09

    fail并不能判断网络请求超时失败,你可以根据statusCode这个来判断

    2020-11-09
    有用
    回复 3
    • 小肥柴的日常(锋)
      小肥柴的日常(锋)
      2020-11-09
      没用,我上面设了5秒超时,然后5秒后走fail function,但是error只返回了1个errMsg参数
      2020-11-09
      回复
    • 小肥柴的日常(锋)
      小肥柴的日常(锋)
      2020-11-09
      我想知道有没有办法能正确把error回调出去,我在外面能做判断
      2020-11-09
      回复
    • this
      this
      2020-11-09回复小肥柴的日常(锋)
      要在外面判断,要先把异步变成同步
      2020-11-09
      回复
登录 后发表内容
问题标签