如何使用ajax从restcontroller打印数据?

ibps3vxo  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(176)

这是我的 index.html :

<script src="/teachers.js"></script>
  <table id="table-content" border='1'>
    <tr>
        <th>Teacher ID</th>
        <th>Teacher Name</th>
        <th>Teacher Position</th>
    </tr>
</table>
``` `teachers.js` :

var service = 'http://localhost/api/v1/';
$(document).ready(function(){
jQuery.support.cors = true;
$.ajax(
{
type: "GET",
url: service + '/teacher/',
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
success: function (data) {

        var trHTML = '';

        $.each(data, function (i, item) {

            trHTML += '<tr><td>' + data.Teacher[i].Name + '</td><td>' + data.Teacher[i].Position + '</td></tr>';
        });

        $('#table-content').append(trHTML);
    },
以及 `TeacherController` :

@RestController
@RequestMapping("/api/v1")
public class TeacherController {
private static final String TEACHER_MODEL = "teacher";
@Autowired
TeacherService teacherService;

@GetMapping("/teacher")
public List fetchAllTeacher() {
return teacherService.getAll();
}

当我得到 `index.html` ,我只有空表,在页面上只能查看表列的名称,没有数据。。
我做错了什么,也许你可以用另一种方法来解决我的问题?
提前感谢您的帮助!!!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题