弹不出json的值
来源:3-6 自由编程
我还差得多
2020-08-19 19:24:38
package com.imooc.ajax.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 jQuery_ajax */ @WebServlet("/jQuery/ajax") public class jQuery_ajax extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public jQuery_ajax() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String butt=request.getParameter("idx"); System.out.println(butt); List list= new ArrayList(); if(butt!=null&&butt.equals("001")) list.add("稻香<br>晴天<br>告别气球<br>"); else if(butt!=null&&butt.equals("002")) list.add("千千厥歌<br>傻女<br>七友<br>"); else if(butt!=null&&butt.equals("003")) list.add("一块红布<br>假行僧<br>新长征路上的摇滚<br>"); String json = JSON.toJSONString(list); response.setContentType("text/html;charset=utf-8"); System.out.println(json); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style type="text/css"> .button { width: 600px; text-align: center; margin: 0 auto; } button { width: 150px; } button:hover { border: 1px solid blue; } </style> </head> <body> <div id="button" class="button"> <button id="001">流行歌曲</button> <button id="002">经典歌曲</button> <button id="003">摇滚歌曲</button> </div> <div id="view"></div> <script type="text/javascript" src="../js/jquery-3.5.1.js"></script> <script type="text/javascript"> var idx; $(function (){ $("button").click(function (){ idx=$(this).attr("id"); $.ajax({ "url":"/ajax/jQuery/ajax", "type":"GET", "data":{"idx":idx}, "dataType": "json", "success": function (json){ alert(json); } }) }) }) </script> </body> </html>
不清楚哪里有错,怎么也弹不出json的值
1回答
同学你好,没有输出json数据,所以获得不到数据,无法弹出json的值。
参考代码如下:
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
相似问题