struts2 +jquery(返回JSON,并遍历JSON)
ajax那弄错了,弄了几个小时,所以写下来,以免今后出错
struts.xml
View Code
1 <package name="product" extends="json-default" namespace="/"> 2 <action name="categoryInfo" class="productAction" method="getCategoryInfo"> 3 <result type="json"> 4 <!-- 需要遍历的action中的属性的名字 --> 5 <param name="root">bookCategorys</param> 6 </result> 7 </action> 8 9 <action name="bookInfo" class="productAction" method="getBookInfo"> 10 <result type="json"> 11 <param name="root">books</param> 12 </result> 13 </action> 14 </package>
jquery中
正确的写法
View Code
1 /* 2 for(var i in res){ 3 //opt = window.Option(res[i].categoryid,res[i].zhname); 4 $("#categoryid").append("<option value="+res[i].categoryid+">"+res[i].zhname+"</option>"); 5 } 6 7 或 8 for(var i = 0 ;i<res.length;i++){ 9 $("#categoryid").append('<option value='+res[i].categoryid+'>'+res[i].zhname+'</option>') 10 } 11 12 或 13 $.each(res, function(i,item){ 14 alert(item.categoryid+","+item.zhname); 15 }); 16 */
错误的写法
View Code
1 for(var i in res){ 2 3 $("#categoryid").append("<option value="+res[i][1]+">"+res[i][0]+"</option>"); 4 }
错误结果:
小计:struts的配置文件的root参数一定要配置
一步一个脚印,方便自己复习,欢迎大家指正,非常感谢,共同进步!
posted on 2012-08-20 22:52 lovebeauty 阅读(1005) 评论(0) 编辑 收藏 举报