request偶尔会请求失败,Initial connection高达20秒
发布于 5 年前 作者 chao81 12478 次浏览 来自 问答

//app.js

App({

 onLaunch: function() { //当小程序初始化完成时,会触发 onLaunch(全局只触发一次)

   var that = this;

   wx.login({

     success: function(res) {

       var code = res.code;

       console.log(‘初始化界面code==:’ + code);

       //请求服务器获取用户id

       console.log(’==============app执行网络请求===================’);

       wx.request({

         url: getApp().globalData.serverUrl + ‘jdserver/login’,

         data: {

           code: code,

         },

         method: ‘POST’, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT

         header: {

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

         }, // 设置请求的 header

         success: function(res) {

           console.log(’==============app网络请求成功===================’);

           //创建一个dialog

           that.globalData.openid = res.data.openid;

           that.globalData.role = res.data.role;

           console.log(“登录成功  服务器返回信息openid:” + that.globalData.openid);

           //存储openid

           wx.setStorageSync(‘openid’, res.data.openid)

          //======================================

           if (that.openidReadyCallBack) {

             //在index.js的onLoad的app.userPhoneReadyCallBack即为this.userPhoneReadyCallBack

             //当这个函数存在时,我们调用他,传参userPhone

             that.openidReadyCallBack(that.globalData.openid)

             console.log(“app.js  openidReadyCallBack:” + that.globalData.openid);

           }

           //======================================

           if (res.data.checkeds.length>0) { //成功

             console.log(’===============去主页面===============’)

           } else {

             wx.reLaunch({

               url: ‘/pages/register/register’,

             })

             console.log(’============去register页面=============’);

           }

         },

         fail: function() {

           wx.showModal({

             title: ‘您的网络链接失败!!!’,

             content: ‘请重新刷新?’,

             showCancel: true, //是否显示取消按钮

             cancelText: “否”, //默认是“取消”

             cancelColor: ‘skyblue’, //取消文字的颜色

             confirmText: “是”, //默认是“确定”

             confirmColor: ‘skyblue’, //确定文字的颜色

             success: function(res) {

               if (res.cancel) {

                 //点击取消,默认隐藏弹框

               } else {

                 //点击确定

                 temp.splice(index, 1),

                   that.setData({

                     tempFilePaths: temp,

                   })

               }

             },

             fail: function(res) {}, //接口调用失败的回调函数

             complete: function(res) {}, //接口调用结束的回调函数(调用成功、失败都会执行)

           })

         },

         complete: function() {

         }

       })

     },

     fail: function(res) {

     },

     complete: function(res) {

     }

   });

 },

 onShow: function() { //当小程序启动,或从后台进入前台显示,会触发 onShow

 },

 onHide: function() { //当小程序从前台进入后台,会触发 onHide

 },

 onError: function(msg) { //当小程序发生脚本错误,或者 api 调用失败时,会触发 onError 并带上错误信息

   console.log(“appError:” + msg)

 },

 globalData: { //定义全局对象

   openid: ‘’,

   role: ‘’,

   serverUrl: https://hechigaosuxinxihua.xyz/

   // serverUrl: ‘http://deshengyijilu.iask.in:49043/

 }

})

2 回复

检查一下域名解析,dns等,还有https证书

开发工具实现,网络请求延时

回到顶部