Autrui 2021-05-23 22:43 采纳率: 50%
浏览 379
已采纳

后台数据传过来,但前端没法显示

这个queryStudent.do是从后端读数据库传过来的数据

package cn.dwg.controller;

import cn.dwg.domain.Student;
import cn.dwg.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
	@Resource
	private StudentService service;
	//注册学生
	@RequestMapping("/addStudent.do")
	public ModelAndView addStudent(Student student){
		ModelAndView mv = new ModelAndView();
		String tips="注册失败";
		//调用service处理student
		int nums=service.addStudent(student);
		if (nums>0){
			//注册成功
			tips="学生["+student.getName()+"]注册成功";
		}
		//添加数据
		mv.addObject("tips",tips);
		//指定结果页面
		mv.setViewName("result");
		return mv;
	}


	//处理查询,响应Ajax
	@RequestMapping("/queryStudent.do")
	@ResponseBody
	public List<Student> queryStudent(){
		//参数检查,简单数据处理
		List<Student> students = service.findStudents();
		return students;
	}
}

这个是controller类

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
	String basePath = request.getScheme() +"://"+
			request.getServerName() + ":" +request.getServerPort() +
			request.getContextPath() + "/";
%>
<html>
<head>
	<title>查询学生Ajax</title>
	<base href="<%=basePath%>"/>
	<script type="text/javascript" src="js/jquery-3.5.1.js"></script>
	<script type="text/javascript">
		$(function () {
			$("#lod").click(function () {
				$.ajax({
					url:"student/queryStudent.do",
					type:"get",
					dataType:"json",
					success:function (data) {

						$.each(data,function (i,n){
							$("info").append("<tr>")
								.append("<td>"+ n.id + "</td>")
								.append("<td>"+ n.name + "</td>")
								.append("<td>"+ n.age + "</td>")
								.append("</tr>")
						})
					},
					error:function (data){
						alert(data)
					}
				})
			})
		})
	</script>
</head>
<body>
	<div align="center">
		<table>
			<thead>
			<tr>
				<td>学号</td>
				<td>姓名</td>
				<td>年龄</td>
			</tr>
			</thead>
			<tbody id="info">

			</tbody>
		</table>
		<input type="button" id="lod" value="查询数据">
	</div>
</body>
</html>

这个是页面的jsp,我是跟着b站springmvc教程打的,老师那里能显示。

我这里也接收到了json,但就是循环不显示数据,希望大佬给我指点一下哪里可能出错了

 

  • 写回答

3条回答 默认 最新

  • 大太子二太子 2021-05-24 09:12
    关注

    你前端里info是啥?如果success函数执行了,也拿到数据了,那就是在前端没有找到info对象。如果前端有id=info的table的话,jquery用id获取对象用$("#info")

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效