wx.request为什么无法起作用?

小程序 文章 2021-02-08 22:41 460 0 全屏看文

AI助手支持GPT4.0

wx.request为什么无法起作用?Why can't wx.request work?

wx.request为什么无法起作用?
如下代码在开发工具真机测试时,日志只能打印到console.log("app.globalData.wxcode---------------" + app.globalData.wxcode);后面wx.request没有任何反馈,服务器端也无任何调用记录。但是在开发工具一开始加载本项目时,是正常可以获取服务端数据的,百思不得其解,请大佬们解惑,困扰一天了。



Page({  data: {    canIUse: wx.canIUse('button.open-type.getUserInfo'),  },  onLoadfunction (e{   //加载入口    console.log('首页开始加载。。。')    //custId -----: ' + custId)    var that = this    console.log(app.globalData)    console.log('全局变量');    // if (!app.globalData.userInfo) {    if (1) {      console.log('进入')      var that = this;      wx.getSetting({        success (res){          if (res.authSetting['scope.userInfo']) {            wx.getUserInfo({              successfunction(res{                console.log(res.userInfo)                that.setData({                  userInfo: res.userInfo                })                app.globalData.userInfo=res.userInfo;                console.log("新测试0" + app.globalData.wxcode);                console.log("新测试1" + res.userInfo.avatarUrl);                console.log("新测试2" + app.globalData.userInfo.avatarUrl);                            let obj = {};                   console.log(app.globalData.userInfo);                   //头像                   obj.avatarUrl = app.globalData.userInfo.avatarUrl;                   //性别                   obj.gender = app.globalData.userInfo.gender;                   //昵称                   obj.nickName = app.globalData.userInfo.nickName;                   //国家                   obj.country = app.globalData.userInfo.country;                   //省份                   obj.province = app.globalData.userInfo.province;                   //城市                   obj.city = app.globalData.userInfo.city;                   console.log(obj);                   console.log("app.globalData.wxcode---------------" + app.globalData.wxcode);                              wx.request({                     url: properties.getURL() + '/test/getOpenId.do',                          data: {                       wxcode: app.globalData.wxcode,                       nickName: app.globalData.userInfo.nickName,                       city: app.globalData.userInfo.city,                       avatarUrl: app.globalData.userInfo.avatarUrl,                       gender: app.globalData.userInfo.gender,                       fatherCustId:e.custId,                       fatherName:e.custNickname                     },                     header: {                       'content-type''application/json' // 默认值                     },                     success(res) {                       //success: function (res) {                       console.log("res.responseCode : " + res.data.responseCode)                       if (res.data.responseCode == 0) {                         console.log("data  ffffffff  : " + JSON.stringify(res.data.data))                         app.globalData.custid = res.data.data.user.custid                         console.log(" app.globalData.custid  ffffffff  : " + JSON.stringify(app.globalData.custid))                       }                     }                                         })              }            })          }        }      })        //})            }    

The following code can only be printed to the console.log (" app.globalData.wxcode ---------------" +  app.globalData.wxcode ); behind wx.request There is no feedback, and there is no call record on the server side. However, when the development tool started to load this project, it was normal to get the server-side data. I couldn't think of the solution. Please solve the puzzle for a day. Page({  data: {    canIUse:  wx.canIUse (' button.open - type.getUserInfo ',}, onload: function (E) {/ / load entry console.log ('home page starts loading... ')    //custId -----: ' + custId)    var that = this     console.log ( app.globalData )     console.log ('global variable '); / / if (! app.globalData.userInfo ) {    if (1) {       console.log ('enter ') var that = this;        wx.getSetting ({        success (res){          if ( res.authSetting [' scope.userInfo ']) {             wx.getUserInfo ({              success: function(res) {                 console.log ( res.userInfo )                 that.setData ({                   userInfo:  res.userInfo                 })                 app.globalData.userInfo=res .userInfo;                 console.log (new test 0 + app.globalData.wxcode );                 console.log (new test 1) + res.userInfo.avatarUrl );                  console.log (new test 2 + app.globalData.userInfo .avatarUrl);                            let obj = {};                    console.log ( app.globalData.userInfo ); / / head portrait obj.avatarUrl  =  app.globalData.userInfo . avatarurl; / / gender obj.gender  =  app.globalData.userInfo .gender; / / nickname obj.nickName  =  app.globalData.userInfo . nickname; / / Country obj.country  =  app.globalData.userInfo .country; / / Province obj.province  =  app.globalData.userInfo . province; / / City obj.city  =  app.globalData.userInfo .city;                    console.log (obj);                     console.log (" app.globalData.wxcode ---------------" +  app.globalData.wxcode );                               wx.request ({                     url:  properties.getURL () + '/test/ getOpenId.do ',                           data: {                       wxcode:  app.globalData.wxcode ,                       nickName:  app.globalData.userInfo .nickName,                       city:  app.globalData.userInfo .city,                        avatarUrl:  app.globalData.userInfo .avatarUrl,                       gender:  app.globalData.userInfo .gender,                       fat herCustId:e.custId ,                        f atherName:e.custNickname }, header: {content type ':'application / JSON' / / default},                      success(res) {                       //success: function (res) {                        console.log (" res.responseCode  : " +  res.data.responseCode )                       if ( res.data.responseCode  == 0) {                           console.log ("data  ffffffff  : " +  JSON.stringify ( res.data.data ))                          app.globalData.custid  =  res.data.data . user.custid                           console.log ("  app.globalData.custid   ffffffff  : " +  JSON.stringify ( app.globalData.custid ))                        }                     }                                         })              }            })          }        }      })        //})            }    

回答:

💡:

wx.request 加个 fail 回调输出错误信息看下

Mr.Zhao:

没配置request合法域名

常林:

这是真机测试非正常加载的日志

-EOF-

AI助手支持GPT4.0