jQuery遍历JSON对象,直接看代码:
本文在ajax中调用struts2 action ,查询数据库,然后返回字符串,演示返回script,text,json类型的数据的用法
本文相关代码下载在
具体下载目录在 /2011年资料/jQuery教程资料/jQuery调用struts2/
一。返回script和text时代码都一样
- ajax-jquery.js
- function commonAjax(oper,prod,url){
- oper.bind("change",function(){comJquery(oper,prod,url)});
- prod.bind("change",function(){comJquery(oper,prod,url)});
- }
- function comJquery(oper,prod,url){
- var prodId=prod.val();
- if(oper.val()!=''&&prod.val()!=''&&prod.val()!=0&&prod.val()!=-1){
- jQuery.ajax({
- url : url,
- data : {productId : prodId},
- type : "post",
- cache : false,
- dataType : "script"或者"text",
- success:callback
- });
- }else{
- $("#company").html('');
- }
- }
-
- function callback(data){
- $("#company").html(data);
- }
-
-
- jsp页面调用ajax js(不管返回什么类型,调用的代码都一样)
- <script type="text/javascript" src="script/jquery.js"></script>
- <script type="text/javascript" src="script/json2.js"></script>
- <script type="text/javascript" src="script/ajax-jquery.js"></script>
-
- var op=$("#cbApplySubmit_changeApplyFormBO_operationId");
- var pr=$("#cbApplySubmit_changeApplyFormBO_productId");
- var url="${contextPath}/assets/businessChange/ajaxGetCompany.do";
- commonAjax(op,pr,url);
-
-
-
- struts2 action
-
- private Integer productId;
- private IProductMng productMng;
-
-
-
- public void ajaxGetCompany() throws Exception {
- ProductBO prod = productMng.loadProduct(productId);
- Integer companyId = prod.getCompanyId();
- CompanyBO comp = productMng.loadCompany(companyId);
- String message = "事业部为:" + comp.getName();
- sendMsg(message);
- }
-
- public void sendMsg(String content) throws IOException{
- HttpServletResponse response = ServletActionContext.getResponse();
- response.setCharacterEncoding("UTF-8");
- response.getWriter().write(content);
- }