How to get a list from mvc controller to view using jquery ajax

前端 未结 6 1917
孤街浪徒
孤街浪徒 2021-02-04 08:02

I need to get a list from mvc controller to view using jquery ajax. how can i do that. this is my code. Its alerting error message.

In Controller

 publ         


        
6条回答
  •  野的像风
    2021-02-04 08:18

       $(document).ready(function () {
            var data = new Array();
            $.ajax({
                url: "list",
                type: "Get",
                data: JSON.stringify(data),
                dataType: 'json',
                success: function (data) {
                    $.each(data, function (index) {
                        // alert("id= "+data[index].id+" name="+data[index].name);
                        $('#myTable tbody').append("" + data[index].id + "" + data[index].name + "");
                    });
    
                },
                error: function (msg) { alert(msg); }
            });
        });
    


        @Controller
        public class StudentController
        {
    
            @Autowired
            StudentService studentService;
            @RequestMapping(value= "/list", method= RequestMethod.GET)
    
            @ResponseBody
            public List dispalyPage()
            {
    
                return studentService.getAllStudentList();
            }
        }
    

提交回复
热议问题