老师您好,请问为什么会显示undefined??
老师您好,我想问一下为什么会显示undefined,如果将var html初始化为空字符串,虽然不会再显示undefined,但是会显示第一行为空白,请问这是为什么?我使用console.log()打印出来的是正常的。谢谢老师!
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> <style type="text/css"> #div1{ position:absolute; right:1050px; } #div2{ position:absolute; right:525px; } #div3{ position:absolute; right:10px; } #div4{ position:absolute; top:40px; right:730px; } input{ width:450px; } </style> </head> <body> <script type="text/javascript" src = "/ajax/js/jquery-3.4.1.js"></script> <div id="div1"> <input id = "button1" type="button" value="员工列表"> </div> <div id="div2"> <input id = "button2" type="button" value="职位列表"> </div> <div id="div3"> <input id = "button3" type="button" value="部门列表"> </div> <div id="div4" style = "text-align:center"></div> </body> <script type="text/javascript"> var xmlhttp; var text; var empName; if(window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); }else{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } $("#button1").click(function(){ xmlhttp.open("GET","http://localhost:8080/ajax/ajax",true); xmlhttp.send(); xmlhttp.onreadystatechange=function(){ if(xmlhttp.status == 200 && xmlhttp.readyState == 4){ text = xmlhttp.responseText; //var empList = JSON.parse(text); var empList = eval("("+text+")"); console.log(empList); var html; for(var i=0;i<empList.length;i++){ empName = empList[i]; console.log(empList[i].name); html = html + "<br>" + empName.name + i; } $("#div4").html(html); } } }); </script> </html>
5
收起
正在回答
3回答
1、undefined表示不能识别的类型。如果值为null时,在el表达式中会显示undefined
2、同学把html设置为“”后,就可以识别了,但是因为同学加了换行<br>
所以第一行是空行,
建议同学吧<br> 加到最后试试。
html = html + empName.name + i+"<br>";
3、如果还是有问题,请同学贴一下你的代码:
如果贴代码,
注意不要贴在回复里,会失去代码的格式。可以在回答里贴一下。祝学习愉快。
慕粉121948417
2019-11-02 13:38:14
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> <style type="text/css"> #div1{ position:absolute; right:1050px; } #div2{ position:absolute; right:525px; } #div3{ position:absolute; right:10px; } #div4{ position:absolute; top:40px; right:730px; } input{ width:450px; } </style> </head> <body> <script type="text/javascript" src = "/ajax/js/jquery-3.4.1.js"></script> <div id="div1"> <input id = "button1" type="button" value="员工列表"> </div> <div id="div2"> <input id = "button2" type="button" value="职位列表"> </div> <div id="div3"> <input id = "button3" type="button" value="部门列表"> </div> <div id="div4" style = "text-align:center"></div> </body> <script type="text/javascript"> var xmlhttp; var text; if(window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); }else{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } $("#button1").click(function(){ xmlhttp.open("GET","http://localhost:8080/ajax/ajax",true); xmlhttp.send(); xmlhttp.onreadystatechange=function(){ if(xmlhttp.status == 200 && xmlhttp.readyState == 4){ text = xmlhttp.responseText; var empList = JSON.parse(text); //var empList = eval("("+text+")"); console.log(empList); var html; for(var i=0;i<empList.length;i++){ var empName = empList[i]; console.log(empList[i].name); html = html + empName.name + i+ "<br>"; } // $("#div4").html(html); document.getElementById("div4").innerHTML = html; } } }); $("#button2").click(function(){ xmlhttp.open("GET","http://localhost:8080/ajax/ajax",true); xmlhttp.send(); xmlhttp.onreadystatechange=function(){ if(xmlhttp.status == 200 && xmlhttp.readyState == 4){ text = xmlhttp.responseText; //var empList = JSON.parse(text); var empList = eval("("+text+")"); console.log(empList); var html; for(var i=0;i<empList.length;i++){ var empJob = empList[i]; //console.log(empList[i].name); html = html + "<br>" + empJob.job + i; } $("#div4").html(html); } } }); $("#button3").click(function(){ //发送Ajax请求 xmlhttp.open("GET","http://localhost:8080/ajax/ajax",true); xmlhttp.send(); //处理服务器响应 xmlhttp.onreadystatechange=function(){ if(xmlhttp.status == 200 && xmlhttp.readyState == 4){ text = xmlhttp.responseText; //var empList = JSON.parse(text); var empList = eval("("+text+")"); console.log(empList); var html; for(var i=0;i<empList.length;i++){ var empDept = empList[i]; //console.log(empList[i].name); html = html + "<br>" + empDept.dept + i; } $("#div4").html(html); } } }); </script> </html>
老师您好,上面是jsp文件当中的代码,可是还是有问题
慕粉121948417
2019-11-02 13:36:10
package com.imooc.ajax; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.alibaba.fastjson.JSON; import com.imooc.employee.Employee; public class Ajax extends HttpServlet{ @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //response.getWriter().println("test"); String empName ; Employee emp1 = new Employee("小红", "职员", "人事部"); Employee emp2 = new Employee("小明", "职员", "技术部"); Employee emp3 = new Employee("小白", "经理", "事业部"); List<Employee> empList = new ArrayList<Employee>(); //List<String> nameList = new ArrayList<String>(); empList.add(emp1); empList.add(emp2); empList.add(emp3); // for(Employee emp:empList) { // empName = emp.getName(); // nameList.add(empName); // // } // String jsonName = JSON.toJSONString(nameList); String jsonEmp = JSON.toJSONString(empList); response.setContentType("text/html;charset=utf-8"); response.getWriter().println(jsonEmp); } }
老师您好,我将换行换到最后面了,但是还是现实undefined,这个是Java代码
2. 从网页搭建入门JavaWeb
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星