V2EX POST
649 subscribers
138 photos
31 videos
99 files
104K links
v2ex新贴定时推送,可按需置顶本频道然后关闭通知
Download Telegram
小程序 globalData undefined 如何解决

```//封装 wx.request()
function request(requestMapping, data, requestWay, contentType) {
wx.showLoading({
title: '请稍后',
})
return new Promise(function(resolve, reject) {
console.log('请求中。。。。。')
wx.request({
url: '自己的服务器地址' + requestMapping,
data: data,
header: {
'content-type': contentType // 默认值
},
timeout: 3000,
method: requestWay,
success(res) {
//console.log(res)
if (res.data.success == false || res.data.statusCode == 404) {
reject(res)
} else {
resolve(res)
}
},
fail: (e) => {
wx.showToast({
title: '连接失败',
icon: 'none'
})},
complete: () => {
wx.hideLoading()
}
})
})
}

//获取 openid
function getOpenId(app, that){
return new Promise(function (resolve, reject) {
wx.login({
success: function (yes) {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
var requestMapping = '/testopenid'
var data = {
code: yes.code
}
var requestWay = 'GET'
var contentType = 'application/json'
var p =request(requestMapping, data, requestWay, contentType)
p.then(res => {
//console.log(res) 做一些后续操作
app.globalData.openId = res.data;
resolve(res)
}).catch(e => {
reject(e)
})
},
fail(e) {
console.log(e)
}
})
})
}
```

getOpenId(getApp(),this);


![RUNOOB 图标]( http://mmbiz.qpic.cn/mmbiz_png/p6SkCZiajBT3w5FhP7Zib2VRXQiahCicPgH5JqrmpocC3DRKBqIuDM1IRphCYNUoVANMfRBD2XYwOQiaKh0XJicAianuA/0?wx_fmt=png)

#res #data #wx #function #var #request #requestMapping #requestWay #contentType #resolve