为什么页面获取不到json里面title那些属性,,全都是undefined
来源:3-4 Ajax函数的使用
SomnusL
2020-04-08 01:18:41
package ajax;
public class News {
private String title;
private String date;
private String source;
private String content;
public News(String title, String date, String source, String content) {
this.title = title;
this.date = date;
this.source = source;
this.content = content;
}
public News() {
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
package ajax;
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 NewsListServlet
*/
@WebServlet("/NewsList")
public class NewsListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public NewsListServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
String type=request.getParameter("t");
List list=new ArrayList();
if(type!=null && type.equals("pypl")) {
list.add(new News("pypl:2018bbc","2018-5-6","pypl","..."));
list.add(new News("pypl:2019bbc","2018-5-6","pypl","..."));
list.add(new News("pypl:2201bbc","2018-5-6","pypl","..."));
list.add(new News("pypl:2048bbc","2018-5-6","pypl","..."));
}else if(type==null || type.equals("toibe")) {
list.add(new News("toible:2018bbc","2018-5-6","tobile","..."));
list.add(new News("toible:2019bbc","2018-5-6","tobile","..."));
list.add(new News("toible:2201bbc","2018-5-6","tobile","..."));
list.add(new News("toible:2048bbc","2018-5-6","tobile","..."));
}
String json=JSON.toJSONString(list);
response.setContentType("text/html;charset=utf-8");
response.getWriter().println(json);
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-3.4.1.js"></script>
<script type="text/javascript">
$(function(){
$.ajax({
"url":"/ajax/NewsList",
"type":"get",
"data":"t=toibe",
"dataType":"text",
"success":function(json){
console.log(json);
for(var i=0;i<json.length;i++){
$("#container").append("<h1>"+json[i].title+"</h1>");
}
}
})
})
</script>
</head>
<body>
<div id="container">
</div>
</body>
</html>
1回答
好帮手慕小尤
2020-04-08
同学你好, "dataType":"text",返回纯文本字符串。无法获取到数据,则建议同学将返回类型修改为json("dataType":j"son":,返回 JSON 数据 )。修改后代码如下所示:

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
相似问题