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

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

这个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条)

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格