JSON 元素未显示

json element is not displaying

本文关键字:显示 元素 JSON      更新时间:2023-09-26

我是EJS的新手,我有这样的输入标签

<input class="form-control" id="inputName" type="text" <% { %>value='<%= JSON.stringify(result.firstname) %>'<% } %>

此处,打印数据为空。

但是如果我给出这样的值:

<input class="form-control" id="inputName" type="text" <% { %>value='<%= JSON.stringify(result) %>'<% } %>

它像这样打印整个 JSON 数据

[{"userid":526,"firstname":"asdw","lastname":null,"username":"aasdasd","password":"sadadsadsadasda","email":"asdasf.m@asd.com","contact":null}]

我在这里做错了什么?

你的 json 对象是一个数组,试试这个:JSON.stringify(result[0].firstname) .

要返回不是数组的 json 对象,也许您应该在后端进行一些修改。