【发布时间】:2018-07-12 07:35:45
【问题描述】:
我有一个 springboot 应用程序,我使用 thymeleaf 作为前端。尝试在单击提交按钮时进行 ajax 调用。
这是我的控制器代码:
@RequestMapping(value="/person", method = RequestMethod.GET)
public String testing(@RequestParam(value = "objectId") String groupObjectId, Model model) throws PersistenceException{
Long objectID = Long.parseLong(objectId);
User User = userService.findUser(objectID);
model.addAttribute("user",user);
return "userList::userList";
}
这是我的 javascript ajax 调用:
function getUserInfo(groupObjectId){
$.ajax({
type : "GET",
url : "/person"+"?objectId="+objectId,
timeout : 100000,
success : function(result) {
console.log("SUCCESS: ", result);
$("#userListPanel").show();
},
error : function(e) {
console.log("ERROR: ", e);
},
done : function(e) {
console.log("DONE");
}
});
我有一个包含不同片段的索引页面:
<div th:replace="organizationList :: organizationList"
th:remove="tag"></div>
<div th:replace="userList :: userList"
th:remove="tag"></div>
我在组织列表中的一个字段上定义了 onclick 事件:
<td class="col-xs-2"><a th:id="${org.name}" th:text="${org.name}" th:href="'javascript:getUserInfo(\'' + ${org.objectID} + '\');'"></a></td>
我的通话成功了,我在 Chrome 控制台中看到了如下结果以及该面板中的其他内容:
<tr>
<td><a href="#">Username1</a></td>
<td>firstName</td>
<td>LastName</td>
</tr>
问题是这些更改没有反映在我的页面中。面板返回为空。我的 ajax 调用中有什么遗漏吗?完成我的 ajax 调用后,我还尝试了以下这些选项:
$("userListPanel").append(result);
$("userListPanel").html(result);
或者有什么方法可以检索我在模型中设置的值?
这是我原来的 userList 模板:
<div xmlns:th="http://www.thymeleaf.org" th:fragment="userList" th:remove="tag">
<div id="userListPanel" class="panel panel-primary collapse">
<div class="panel-heading">
<div class="panel-title">
<span data-toggle="collapse" data-target="#userListBody" class="glyphicon glyphicon-minus"></span>
Organization: orgA
<span class="panel-title pull-right"> No. of Documents: 3</span>
</div>
</div>
<div id="userListBody" class="panel-body panel-collapse panel-body-resizable collapse in panel-body-fixed-height paddingTop0"
th:fragment="resultsList">
<table class="table table-striped table-hover" id="userListTable">
<thead>
<tr>
<th>Name</th>
<th>DOB</th>
<th>Status</th>
</tr>
</thead>
<tbody id="userListTableBody">
<tr th:each="user:${userList}">
<td th:text="${user.name}"></td>
<td th:text="${user.dob}"></td>
<td th:text="${user.status}"></td>
</tr>
</tbody>
</table>
</div>
</div>
【问题讨论】:
标签: ajax spring-boot thymeleaf