search
HomeWeChat AppletMini Program DevelopmentWhat should I do if the WeChat applet network times out?

What should I do if the WeChat applet network times out?

Jul 14, 2020 pm 03:08 PM
WeChat appletnetwork timeout

Solution to the WeChat applet network timeout: 1. If the task times out when running onLaunch, set the error level to 0 and redirect to the error page; 2. If the page request times out, set the error level to 0 2. Can be fixed by retrying.

What should I do if the WeChat applet network times out?

Solution to WeChat applet network timeout:

onLaunchThrough this we can Obtain the user's basic information, or position it for next step processing. If the data cannot be obtained, the entire applet will fail.

So I suggest that errors can be divided into two levels. If the developer server cannot be connected, it can be handled by reloading the page. However, if the data in onLaunch cannot be obtained, the user must exit the small Program, reopen it and try again. The configuration in

app.json is used to set the timeout, the default is 6000 milliseconds, which is 6 seconds

"networkTimeout": {
    "request": 6000,
    "downloadFile": 10000
  }

Related learning recommendations: WeChat Mini program development tutorial

1. If the task times out when running onLaunch, I set the error level to 0 and redirect to the error page

wx.login({
      success(res) {
        if (res.code) {
          //console.log(res.code);
          //发起网络请求
          wx.request({
            url: 'https://**/index/zz/getuserinfo',
            data: {
              code: res.code
            },
            success: res => {
            
              wx.setStorageSync('open_id', res.data.openid);
              wx.setStorageSync('session_id', res.data.session_id);
              wx.setStorageSync('session_key', res.data.session_key);
              that.globalData.isSessionkey=true;
              //console.log(res.data);
              if (that.sessionCallback) {
                        that.sessionCallback(res);
              }
             
            },fail:f=>
            {
              wx.showModal({
                title: '提示',
                showCancel: false,
                content: '可能网络不太好,请重试!',
                success: function () {
                  wx.navigateTo({
                    url: '/pages/reload?error=0'
                  });
                }
              });
            }
          })
        } else {
          console.log('登录失败!' + res.errMsg)
        }
      
      }, fail: function () {
        wx.showModal({
          title: '提示',
          showCancel: false,
          content: '可能网络不太好,请重试!',
          success: function () {
            wx.navigateTo({
              url: '/pages/reload?error=0'
            });
          }
        });
      }
    });

2. If the page request times out, I set the error as 2, which can be fixed by retrying

wx.request({
    url: webUrl + model.url,
    data: model.param,
    method: model.method,
    success: function (res) {
 
    },
    fail: function (res) {
      wx.hideLoading();
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '可能网络不太好,请重试!',
        success: function () {
          wx.navigateTo({
            url: '/pages/reload?error=1'
          });
        }
      });
    }
  })

3. Process the page: Use getCurrentPages() to get the previous page object, Must use wx.navigateToredirect to this page

/**
  * 页面的初始数据
  */
 data: {
  error:0 // 0:需要退出小程序 1:可以重新发起网络请求重试
 },
 reLoad:function(error)
 {
   var pages = getCurrentPages();//获取页面栈
   if (pages.length > 1) {
     //上一个页面实例对象
     var prePage = pages[pages.length - 2];
     let url=prePage.route;
     var options = prePage.options //如果要获取url中所带的参数可以查看options
     console.log('options', options);
     //拼接url的参数
     var urlWithArgs = url + '?'
     for (var key in options) {
       var value = options[key]
       urlWithArgs += key + '=' + value + '&'
     }
     urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)
     
     wx.reLaunch({
       url: '/' + urlWithArgs,
       fail:function(e)
       {
         wx.switchTab({
           url: '/' + prePage.route,
         })
       }
     });
   }
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ error: options.error});
   // this.reLoad(options.error);
  },

The above is the detailed content of What should I do if the WeChat applet network times out?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.