日期:2014-05-19 浏览次数:20803 次
public String execute() {
List<Dept> depts = null;
Map<String,String> dept_map = new HashMap<String,String>();
try {
out = response.getWriter();
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
depts = baseService.find("from Dept d");
if (depts.size() > 0) {
for (int i=0;i<depts.size();i++) {
Dept d = depts.get(i);
dept_map.put(d.getId().toString(),d.getName());
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
System.out.println(dept_map);//查看MAP的数据
System.out.println(JSONSerializer.toJSON(dept_map));//查看生成的json数据
out.print(JSONSerializer.toJSON(dept_map));
}
return null;
$.post("QueryMenu.action","",function(data) {
alert(data);
$.each(data,function(idx,item) {
alert("the item is:" + item);
alert("the key is:" + item.key);
if (idx == 0 )
return true;
$("<option value="+item.key + ">"+ item.value + "</option>").appendTo(".dept");
alert("the value is :" + item.value);
});
},"json");
$.post("QueryMenu.action","",function(data) {
$.each(data,function(key,value) {
alert("the key is:" + key);
$("<option value="+key + ">"+ value + "</option>").appendTo(".dept");
alert("the value is :" +value );
});
},"json");
------解决方案--------------------
用fireBug在前台页面看哈你服务器端返回的究竟是什么东西,然后根据返回数据的格式,来写js就不会有问题了。
------解决方案--------------------
data['1'],data['2']这种格式
------解决方案--------------------
dateType=json或者用$.getJSON()方法
------解决方案--------------------
ajax请求时要设置
$.ajax{
type:json,
......
}
你用 string JSON.stringify(string)弹出alert(string)看看得到什么结果
可以用 string JSON.stringify(string) 转换试试