歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> jQuery調用struts2,返回script/text/json格式的數據

jQuery調用struts2,返回script/text/json格式的數據

日期:2017/3/1 11:09:29   编辑:Linux編程

jQuery遍歷JSON對象,直接看代碼:

本文在ajax中調用struts2 action ,查詢數據庫,然後返回字符串,演示返回script,text,json類型的數據的用法

本文相關代碼下載在

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2011年資料/jQuery教程資料/jQuery調用struts2/

一。返回script和text時代碼都一樣

  1. ajax-jquery.js
  2. function commonAjax(oper,prod,url){
  3. oper.bind("change",function(){comJquery(oper,prod,url)});
  4. prod.bind("change",function(){comJquery(oper,prod,url)});
  5. }
  6. function comJquery(oper,prod,url){
  7. var prodId=prod.val();
  8. if(oper.val()!=''&&prod.val()!=''&&prod.val()!=0&&prod.val()!=-1){
  9. jQuery.ajax({
  10. url : url,
  11. data : {productId : prodId},
  12. type : "post",
  13. cache : false,
  14. dataType : "script"或者"text",
  15. success:callback
  16. });
  17. }else{
  18. $("#company").html('');
  19. }
  20. }
  21. function callback(data){
  22. $("#company").html(data);
  23. }
  24. jsp頁面調用ajax js(不管返回什麼類型,調用的代碼都一樣)
  25. <script type="text/javascript" src="script/jquery.js"></script>
  26. <script type="text/javascript" src="script/json2.js"></script>
  27. <script type="text/javascript" src="script/ajax-jquery.js"></script>
  28. var op=$("#cbApplySubmit_changeApplyFormBO_operationId");
  29. var pr=$("#cbApplySubmit_changeApplyFormBO_productId");
  30. var url="${contextPath}/assets/businessChange/ajaxGetCompany.do";
  31. commonAjax(op,pr,url);
  32. struts2 action
  33. private Integer productId;
  34. private IProductMng productMng; // 通過spring注入的service
  35. // get set
  36. public void ajaxGetCompany() throws Exception {
  37. ProductBO prod = productMng.loadProduct(productId);
  38. Integer companyId = prod.getCompanyId();
  39. CompanyBO comp = productMng.loadCompany(companyId);
  40. String message = "事業部為:" + comp.getName();
  41. sendMsg(message);
  42. }
  43. public void sendMsg(String content) throws IOException{
  44. HttpServletResponse response = ServletActionContext.getResponse();
  45. response.setCharacterEncoding("UTF-8");
  46. response.getWriter().write(content);
  47. }
Copyright © Linux教程網 All Rights Reserved