用$.getJSON方法取不到服务器的值。。。。纠结了两天了

妙_snail_1127 发布于 2014/05/29 08:29
阅读 2K+
收藏 0

1.我先写了个servlet,主要是放一些数据:

public class GetData extends HttpServlet {
private static final long serialVersionUID = 1L;
ArrayList<Info> info1 = new ArrayList<Info>();
ArrayList<Info> info2 = new ArrayList<Info>();
ArrayList<Info> info3 = new ArrayList<Info>();
HashMap<String, ArrayList<Info>> map = new HashMap<String, ArrayList<Info>>();
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
//初始化数据
info1.add(new Info("上海","java",3000));
info1.add(new Info("北京","vb",4000));
info1.add(new Info("广州","vc",5000));

info2.add(new Info("上海","vc",6000));
info2.add(new Info("北京","java",7000));
info2.add(new Info("广州","vb",8000));

info3.add(new Info("上海","vb",9000));
info3.add(new Info("北京","vc",10000));
info3.add(new Info("广州","java",8100));

map.put("高中", info1);
map.put("大专", info2);
map.put("本科", info3);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 返回所需数据
        response.setContentType("text/html;charset=UTF-8");
        JSONObject json = JSONObject.fromObject(map); // 觉得这样写后,传到eCharts.jsp页面上的数据                                                                              //就是json类型了

        HttpSession session = request.getSession();        
        session.setAttribute("status", json);
        request.getRequestDispatcher("charts/eCharts.jsp").forward(request, response);
       
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}

当我运行GetData这个servlet时,页面上显示:

{
"本科":
[{"address":"上海","language":"vb","salse":9000},
{"address":"北京","language":"vc","salse":10000},
{"address":"广州","language":"java","salse":8100}],

"大专":
[{"address":"上海","language":"vc","salse":6000},
{"address":"北京","language":"java","salse":7000},
{"address":"广州","language":"vb","salse":8000}],

"高中":
[{"address":"上海","language":"java","salse":3000},
{"address":"北京","language":"vb","salse":4000},
{"address":"广州","language":"vc","salse":5000}]
}



2.我的eCharts.jsp:

<html>
<head>
<title>eCharts 例子</title>
<script type="text/javascript" src="../chartsjs/esl.js"></script>
<script language="javaScript" src="../js/jquery.js"></script>
<script language="javaScript" src="../js/json2.js"></script>
<script language="javaScript" src="../js/json2-min.js"></script>
</head>
<body>
 <div>${status}</div>
<div id="result"></div>

<script>

 $.getJSON(
'GetData', function(data){
for(var i in data){
$('#result').append('<p>' + data.本科[i].address + '</p>');
alert(data.本科[i].address);
}  
});

</script>
</body>
</html>

各位大神,谁能帮我看看的,当我debug时,

for(var i in data){
$('#result').append('<p>' + data.本科[i].address + '</p>');
alert(data.本科[i].address);

这一部分就不执行,直接结束了,不知道哪里出了问题??

3.其实我除了想要依次循环出“address”,“language”和"salse"的值,我还想要循环出它的key值(本科,高中,大专),就是不知道怎么描述?

加载中
4
山哥
山哥

呵呵,$.getJSON 是 ajax 方式获取服务器的JSON数据,你的Servlet应该返回的是json字符串,而不是对象。

response.setContentType("application/json;charset=UTF-8");
JSONObject json = JSONObject.fromObject(map);
response.getWriter().write(json.toString() 或  json.toJSONString());



0
Jack_Q
Jack_Q

楼上的正解

0
wangchenfeng
wangchenfeng
正解楼上!
0
itwarcraft
itwarcraft
不能同意1楼更多。
0
雨翔河
雨翔河
同意楼上
0
Ryan-瑞恩
Ryan-瑞恩

楼上说了,,,,那我就不说了。。。

你在回调里面弹一下data看看data是个什么东西。。。。。

下面附上$.getJSON()的API:

  • url
    Type:  String
    A string containing the URL to which the request is sent.
  • data
    Type:  PlainObject
    A plain object or string that is sent to the server with the request.
  • success
    Type:  FunctionPlainObject data,  String textStatus,  jqXHR jqXHR )
    A callback function that is executed if the request succeeds.


具体查看官方API:http://api.jquery.com/jQuery.getJSON/

0
一笑居
一笑居
为这个问题纠结2天不值得呀,js出现问题,试试firebug纠错
0
bigtiger02
bigtiger02

引用来自“山哥”的评论

呵呵,$.getJSON 是 ajax 方式获取服务器的JSON数据,你的Servlet应该返回的是json字符串,而不是对象。

response.setContentType("application/json;charset=UTF-8");
JSONObject json = JSONObject.fromObject(map);
response.getWriter().write(json.toString() 或  json.toJSONString());



正解
0
谢小蛋
谢小蛋
eval转一下
返回顶部
顶部