日期:2014-05-16 浏览次数:20359 次
需求是a服务器下页面b.jsp需要调用b服务器的action CoureseInfoProvideAction.java,用了JQuery+ajax技术,返回json类型的数据:
b.jsp:
?
<script type="text/javascript"> function showCourseInfo(){ var resourceCode = document.getElementById("resourceCode").value; jQuery.getJSON("http://10.10.6.31:8099/coureseInfoProvide.jhtml?courseNumber="+resourceCode, function(data) { if(data!="0"){ document.getElementById('resourceName').value = data.courseName; document.getElementById('languageType').value = data.lang; }else{ alert("课件编码不存在!!!"); } }); } </script> <body> <dt> <label><span>*</span> 课件名称:</label> </dt> <dd> <input type="text" class="default_txt" id="resourceName" name="resourceName"/> </dd> <dt> <label><span>*</span> 课件编码:</label> </dt> <dd> <input type="text" class="default_txt" id="resourceCode" name="resourceCode" value="112290_eng"/> <input name="Button2" type="button" class="btn btn_default" onclick="showCourseInfo()" value="Load…" /> </dd> <dt> <label>语言属性:</label> </dt> <dd> <select id="languageType" name="languageType" class="mini_sel"> <option>-请选择-</option> <option value="1">中文</option> <option value="0">英文</option> <option value="-1">其他</option> </select> <input type="text" class="default_txt" id="languageNote" name="languageNote"/> </dd> </body>?
?
CoureseInfoProvideAction.java:
?
public String process() throws Exception { response.setContentType("text/xml;charset=UTF-8"); response.setHeader("Cache-Control", "no-cache"); response.addHeader("Access-Control-Allow-Origin", "*"); response.addHeader("Access-Control-Allow-Headers", "x-requested-with"); PrintWriter out = response.getWriter(); if(StringUtil.isBlank(courseNumber)){ Course course = courseService.getValidCourse(courseNumber); if(course!=null){ StringBuilder sb = new StringBuilder(); String courseName = course.getCourseName(); sb.append("{"); sb.append("\""); sb.append("courseName"); sb.append("\""); sb.append(":"); sb.append("\""); sb.append(courseName); sb.append("\""); Integer lang = course.getLang(); sb.append(","); sb.append("\""); sb.append("lang"); sb.append("\""); sb.append(":"); sb.append("\""); sb.append(lang); sb.append("\""); sb.append("}"); out.print(sb.toString()); course = null; }else{ out.print("0"); } } return null; }?
在CoureseInfoProvideAction.java中增加了response对header的处理,用于对跨域提交参数的支持,在StringBuilder中把字符串组装成了json的格式,在页面便于解析、接收参数。
?
?