Skip to content

Commit 5fcafc1

Browse files
committed
网络请求
1 parent 51f8b15 commit 5fcafc1

File tree

4 files changed

+82
-61
lines changed

4 files changed

+82
-61
lines changed

app.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"pages": [
33
"pages/home/home",
44
"pages/about/about"
5-
]
5+
],
6+
"sitemapLocation": "sitemap.json"
67
}

pages/home/home.js

Lines changed: 50 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,58 @@
11
// pages/home/home.js
2-
Page({
2+
import request from '../../service/network.js'
33

4-
/**
5-
* 页面的初始数据
6-
*/
4+
Page({
75
data: {
86

97
},
10-
11-
/**
12-
* 生命周期函数--监听页面加载
13-
*/
148
onLoad: function (options) {
15-
16-
},
17-
18-
/**
19-
* 生命周期函数--监听页面初次渲染完成
20-
*/
21-
onReady: function () {
22-
23-
},
24-
25-
/**
26-
* 生命周期函数--监听页面显示
27-
*/
28-
onShow: function () {
29-
30-
},
31-
32-
/**
33-
* 生命周期函数--监听页面隐藏
34-
*/
35-
onHide: function () {
36-
37-
},
38-
39-
/**
40-
* 生命周期函数--监听页面卸载
41-
*/
42-
onUnload: function () {
43-
44-
},
45-
46-
/**
47-
* 页面相关事件处理函数--监听用户下拉动作
48-
*/
49-
onPullDownRefresh: function () {
50-
51-
},
52-
53-
/**
54-
* 页面上拉触底事件的处理函数
55-
*/
56-
onReachBottom: function () {
57-
58-
},
59-
60-
/**
61-
* 用户点击右上角分享
62-
*/
63-
onShareAppMessage: function () {
64-
9+
// 1.原生的方式发送网络请求
10+
this.get_data_origin()
11+
12+
// 2.使用封装的request发送网络请求
13+
// Promise最大的好处就是防止出现回调地狱
14+
request({
15+
url: 'http://123.207.32.32:8000/recommend'
16+
}).then(res => {
17+
console.log(res)
18+
}).catch(err => {
19+
console.log(err)
20+
})
21+
},
22+
get_data_origin() {
23+
// 发送网络请求
24+
// 1.发送最简单的get请求
25+
// wx.request({
26+
// url: 'http://123.207.32.32:8000/recommend',
27+
// success: function(res) {
28+
// console.log(res)
29+
// }
30+
// })
31+
// 2.get请求,并且携带参数
32+
// wx.request({
33+
// url: 'http://123.207.32.32:8000/home/data',
34+
// data: {
35+
// type: 'sell',
36+
// page: 1
37+
// },
38+
// success: function (res) {
39+
// console.log(res)
40+
// }
41+
// })
42+
// 3.post请求,并且携带参数
43+
// wx.request({
44+
// url: 'http://httpbin.org/post',
45+
// method: 'post',
46+
// data: {
47+
// name: 'coderwhy',
48+
// age: 18
49+
// },
50+
// success: function(res) {
51+
// console.log(res)
52+
// },
53+
// fail: function(err) {
54+
// console.log(err)
55+
// }
56+
// })
6557
}
6658
})

project.config.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
"ignore": []
55
},
66
"setting": {
7-
"urlCheck": true,
7+
"urlCheck": false,
88
"es6": true,
99
"postcss": true,
1010
"minified": true,
1111
"newFeature": true,
12-
"autoAudits": false
12+
"autoAudits": false,
13+
"checkInvalidKey": true
1314
},
1415
"compileType": "miniprogram",
1516
"libVersion": "2.7.1",

service/network.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export default function request(options) {
2+
return new Promise((resolve, reject) => {
3+
wx.request({
4+
url: options.url,
5+
method: options.method || 'get',
6+
data: options.data || {},
7+
success: resolve,
8+
fail: reject
9+
})
10+
})
11+
}
12+
13+
// export default function request(options) {
14+
// return new Promise((resolve, reject) => {
15+
// wx.request({
16+
// url: options.url,
17+
// method: options.method || 'get',
18+
// data: options.data || {},
19+
// success: function (res) {
20+
// resolve(res)
21+
// },
22+
// fail: function (err) {
23+
// reject(err)
24+
// }
25+
// })
26+
// })
27+
// }

0 commit comments

Comments
 (0)