為啥加入了JSON單機事件出不來報錯啊

為啥加入了JSON單機事件出不來報錯啊

hrml:

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

<input type="button" name="ygName" id="ygName" value="員工列表">

<input type="button" name="bmName" id="bmName" value="部門列表">

<input type="button" name="zyName" id="zyName" value="職業列表">

<div id="container"></div>

<script type="text/javascript">

//創建AJax

var xmlhttp;

if (window.XMLHttpRequest) {

xmlhttp = new XMLHttpRequest();

} else {

xmlttp = new ActiveXObject("Microsoft.XMLHTTP");

}

//啓動

xmlhttp.open("GET", "/JAax/text", true);

xmlhttp.send();

//加載

xmlhttp.onreadystatechange = function() {

if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

var text = xmlhttp.responseText;

//json轉換為String

var json = JSON.parse(text);

console.log(json);

//加入單機事件

$("#ygName").click(function() {

var html = "";

for (var i = 0; i < json.length; i++) {

var company = json[i];

html = html + company.ygName + "<br/>";

}

$("div[id='container']").html(html);

})

}

}

</script>



</body>

</html>

servlet:

package text;


import java.io.IOException;

import java.util.ArrayList;

import java.util.List;


import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import com.alibaba.fastjson.JSON;


/**

 * Servlet implementation class text

 */

@WebServlet("/text")

public class text extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public text() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//先存入list集合裏

List list = new ArrayList();

list.add(new yh("小红", "职员", "人事部"));

list.add(new yh("小明", "职员", "技术部"));

list.add(new yh("小白", "经理", "无线事业部"));

//在八數組轉爲json

String json = JSON.toJSONString(list);

response.setContentType("text/html;charset=UTF-8");

response.getWriter().println(json);

}


}


正在回答

登陆购买课程后可参与讨论,去登陆

2回答

同学你好!

  1. 你贴出的代码,老师加入jquery之后是可以正常显示的

    http://img1.sycdn.imooc.com//climg/5e44b4da09efdeed04070162.jpg

  2. 你加入jquery的地址是否写错了呢?比如:

    http://img1.sycdn.imooc.com//climg/5e44b506094d995f12820137.jpg


    建议你检查一下。如果正确,clean一下项目,清除一下浏览器的缓存。重新启动项目测试一下。

祝学习愉快~

提问者 qq_慕仙7241355 2020-02-12 21:33:16

說錯了是Jquery不是JSON

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师