You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// pages/record/record.js
Page({
/**
* 页面的初始数据
*/
data: {
className: "K班",
students: [
{
id: "102201",
name: "Jhon",
point: 3
},
{
id: "102202",
name: "Kun",
point: 2
},
{
id: "102202",
name: "Tom",
point: 2
},
]
},
onLoad: function(options) {
console.log(options);
// options 参数包含了从上一个页面传递过来的数据
const receivedClassName = options.className; // 获取传递过来的 className默认为空字符串
// 更新页面的数据
this.setData({
className: receivedClassName // 将接收到的 className 赋值给页面的 data 属性
});
// const url = `https://your-backend-server.com/api/students?className=${this.data.className}`
//根据className向后端请求数据
wx.request({
url: `http://192.168.152.1:8090/student/${this.data.className}`,
method: 'GET',
success: function(res) {
// 后端返回的数据格式如下:
// {
// students: [
// { id: "102201", name: "Jhon", point: 3 },
// { id: "102202", name: "Kun", point: 2 },
// { id: "102203", name: "Tom", point: 1 }
// ]
// }
if (res.statusCode === 200 && Array.isArray(res.data)) {
// 使用setData方法更新data内的students数组
this.setData({
students: res.data
});
} else {
console.error('请求成功但数据格式不正确或数据不是数组');
}
}.bind(this), // 注意这里使用.bind(this)来确保this指向当前页面实例
fail: function(err) {
console.error('请求失败', err);
}
});
},
})