1.Struts配置:
<!-- 借款 -->
<package name="loanAjax" namespace="/bns/loanAjax" extends="custom-rest-default"> <action name="*/*" method="{2}" class="cn.ac.sec.match.bns.loan.action.{1}Action"> <result type="stream"> <param name="contentType">text/html</param><!-- 默认为text/plain --> <param name="inputName">inputStream</param><!-- 默认就为inputStream --> </result> </action> </package>
2.脚本代码:
function queryCusmanagerByAjax(){ var cusmanagerId=$('#cusmanagerId').val(); $.ajax({ type:"post",url: $("#contentPath").val()+"/bns/loanAjax/LoanCusmanager/queryByAjax.do?cusmanagerId="+cusmanagerId,dataType:"json",async: false,cache:false,success: function (data) { alert("Ajax请求数据成功!"); alert(data.managerNo); },error: function () { alert("Ajax请求数据失败!"); } }); }
3.Java代码
public void queryByAjax(){ LoanCusmanager lc = loanCusmanagerManager.findByProperty("managerNo",cusmanagerId); HttpServletResponse response = this.getResponse(); PrintWriter out = null; JSONObject jsonObject = JSONObject.fromObject(lc); try { out = response.getWriter(); out.print(jsonObject); } catch (IOException e) { e.printStackTrace(); } }
4.引用的是
<span style="white-space:pre"> </span>import net.sf.json.JSONObject;原文链接:https://www.f2er.com/ajax/163612.html