Java 如何使用AJAX获取表
我试图从数据库中获取数据并在浏览器中显示,所以 这是我的Java 如何使用AJAX获取表,java,jquery,ajax,Java,Jquery,Ajax,我试图从数据库中获取数据并在浏览器中显示,所以 这是我的index.html: <button id="getAllGroups" type="button" class="btn btn-primary">Groups</button> <div class="row"> <div class="col-md-8">
index.html
:
<button id="getAllGroups" type="button" class="btn btn-primary">Groups</button>
<div class="row">
<div class="col-md-8">
<table>
<thead>
<th>Group Id</th>
<th>Group name</th>
</thead>
<tbody id="tbody">
</tbody>
</table>
</div>
</div>
实体组
:
@Entity
@ToString
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "groups", schema = "public")
public class Group {
@Id
@Column(name="group_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int groupId;
@NotNull
@Size(min = 4,message = "Group name should consist of 4 symbols!")
@Size(max = 4)
@Column(name="group_name")
private String groupName;
}
当我点击
index.html
上的Groups
按钮whereid=“getAllGroups”
,什么都不会发生,但当我尝试只打印数据库中的数据,而不是以表格式打印数据时,一切正常,因此,我假设Ajax
中的问题。嗨,结果是什么?你能显示它的输出吗?你能在浏览器>开发工具中看到API调用吗?。你的API和UI URL是什么?@Swati Result有一个由10行组成的数组,就像在我的数据库中一样-我有10个groups.Result或Result.data?为什么不在foreach循环中放一个console.log并调试其中的组内容呢?顺便说一句,我不确定,但它一定不是团体而不是团体吗?@ClausBönnhoff我从数据库中得到了10个团体,我已经检查过了。当我点击index.html
上的按钮Groups
时,我得到了10个组(使用dev browser tools和console.log检查),但组没有出现在页面上。嗨,结果中有什么?你能显示它的输出吗?你能在浏览器>开发工具中看到API调用吗?。你的API和UI URL是什么?@Swati Result有一个由10行组成的数组,就像在我的数据库中一样-我有10个groups.Result或Result.data?为什么不在foreach循环中放一个console.log并调试其中的组内容呢?顺便说一句,我不确定,但它一定不是团体而不是团体吗?@ClausBönnhoff我从数据库中得到了10个团体,我已经检查过了。当我点击index.html
上的按钮Groups
时,我得到了10个组(使用dev browser tools和console.log进行了检查),但组没有出现在页面上
@GetMapping("/checkGroups")
public ResponseEntity<Object> getAllGroups() {
ServiceResponse<List<Group>> response = new ServiceResponse<>("success", groupService.getAll());
return new ResponseEntity<Object>(response, HttpStatus.OK);
}
GET: $(document).ready(
function () {
// GET REQUEST
$("#getAllGroups").click(function (event) {
event.preventDefault();
ajaxGet();
});
// DO GET
function ajaxGet() {
$.ajax({
type: "GET",
url: "checkGroups",
success: function (result) {
if (result.status == "success") {
var custList = "";
$.each(result.data,
function (i, group) {
var Html = "<tr>"
"<td>" + group.groupId + "</td>"
"<td>" + group.groupName + "</td>"
"</tr>";
$('#tbody').append(Html);
});
console.log("Success: ", result);
} else {
console.log("Fail: ", result);
}
},
}
})
@Entity
@ToString
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "groups", schema = "public")
public class Group {
@Id
@Column(name="group_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int groupId;
@NotNull
@Size(min = 4,message = "Group name should consist of 4 symbols!")
@Size(max = 4)
@Column(name="group_name")
private String groupName;
}