从下面的代码中,我试图访问要在前端屏幕上显示的JSON数据。我在nodejs中导出了它,并试图在frontendhtml中的script标记中导入它,但我没有得到数据。有什么方法可以访问这些数据吗?
app.post('/login', async (req, res) => {
let payload = {
"params":{"db":"ExampleDB","login":req.body.email, "password":req.body.password}
};
let result = await axios.post('http://example.net:8088/authenticate', payload);
try{
if (result.data.result.message = 'success') {
res.sendFile(path.join(__dirname,'./public/timesheets.html'))
if (result.data.result.data.is_officer == true){
flag = 1;
}
}
}
catch(ex){
res.send(`<div align ='center'></div><br><br><br><div align ='center'><a
href='./login.html'>Invalid Email or Password Please try again</a></div><br><br>
<div align='center'></div>`);
}
});
我正试图将这个json数据is_officer提取到我的前端html代码中。如何在前端获取这些数据?json-api响应如下
{
"jsonrpc": "2.0",
"id": null,
"result": {
"message": "Success",
"code": 200,
"data": {
"is_officer": true,
"department_id": [
false,
false
]
}
}
}
需要在nodejs和html中编写哪行代码才能在前端html上实现这一点?
使用Axios将JSON从后端带到前端
官方网站:https://axios-http.com/
Cdn:https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js
import axios from "axios";
axios.get('/users')
.then(res => {
console.log(res.data);
});```