jQuery调用struts2,返回script/text/json格式的数据
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; // 通过spring注入的service
- // get set
- 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);
- }
相关推荐
EdwardSiCong 2020-11-23
85477104 2020-11-17
hhanbj 2020-11-17
81427005 2020-11-11
seoppt 2020-09-13
honeyth 2020-09-13
WRITEFORSHARE 2020-09-13
84483065 2020-09-11
momode 2020-09-11
85477104 2020-08-15
83510998 2020-08-08
82550495 2020-08-03
tthappyer 2020-08-03
84901334 2020-07-28
tthappyer 2020-07-25
TONIYH 2020-07-22
tztzyzyz 2020-07-20
83510998 2020-07-18
81463166 2020-07-17