日期:2014-05-16  浏览次数:20680 次

ajax json data应该怎么写
这是jsp页面的ajax
[code=text]<script type="text/javascript">
$(document).ready(function(){
//当一级栏目选项发生变化
$('#type1').change(function(){
$('#type2').empty();
$('#type2').append("<option value='0'>---请选择二级---</option>");
//当一级栏目选项发生变化时,其对应的二级栏目也随着改变

//获取一级栏目id
var type1 = $('#type1').val();
if(type1 != 0){
$.ajax({
type: "GET",
url: "<%=basePath%>type?fid="+type1,
dataType : "json",
data : {},
success: function(data){
alert(data);
var json=eval(data);
console.log(json);
for(var i = 0, len = json.length; i < len; i++){
if(type2 == json[i].fid)
$('#type2').append("<option value='"+json[i].fid+"' selected>"+json[i].name+"</option>");
else
$('#type2').append("<option value='"+json[i].fid+"'>"+json[i].name+"</option>");
}
},
error : function (XMLHttpRequest, textStatus, errorThrows, data){
alert(data);
alert(XMLHttpRequest);
alert(textStatus);
alert(errorThrows);
}
});
}
});
});
</script>
[/code]
它进入了所请求的页面
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.req = request;
this.res = response;
this.session = request.getSession();
//获取父ID为fid的子id
if(req.getParameter("fid") != null){
List<String> list1 = new ArrayList<String>();
int fid = Integer.parseInt(request.getParameter("fid"));
List<Type> list2 = tdao.list(fid);
//JSONArray jsonArray = JSONArray.fromObject(list2);
/*for(Type type : list2){
list1.add(type.getId() +","+type.getName());
}*/
for(Type type : list2){
list1.add(type.getId() +":'"+type.getName()+"'");
// list1.add("type2:'"+type.getId()+"'" +"name:'"+type.getName()+"'");
}
ResponseUtil.write(response, list1);//返回值为      [4:'学院简介', 5:'组织机构', 6:'师资队伍', 7:'学科建设']
} else {
doPost(request, response);
}
}

用firedebug调试时,没有执行success里面的方法,error方法弹出的对话框显示json解析错误。
我想问data里面我该怎么写?有人跟我说后台返回的值要序列化,可是我不知道怎么序列化。还有既然后台有数据,为什么返回的还是为什么没有执行success里面的方法呢?