如何从前端获取 List<string> 类型数据
在前端获取 List
- 使用 JavaScript 的 fetch API 从后端获取数据并解析为 JSON 格式,然后将 List
类型的字段赋值给前端变量。
fetch('url')
.then(response => response.json())
.then(data => {
const list = data.list; // 获取 List<string> 类型的字段
// 处理 list 变量
});
- 在 HTML 中使用 JavaScript 模板引擎(如 Handlebars)渲染数据,将 List
类型的字段传递给模板。
<!-- 模板 -->
<ul>
{{#each list}}
<li>{{this}}</li>
{{/each}}
</ul>
<!-- JavaScript -->
const data = {
list: ['item1', 'item2', 'item3'] // List<string> 类型的字段
};
const template = Handlebars.compile(document.getElementById('template').innerHTML);
const html = template(data);
document.getElementById('container').innerHTML = html;
以上两种方法都可以获取 List
原文地址: https://www.cveoy.top/t/topic/oB1p 著作权归作者所有。请勿转载和采集!