我的action能响应ajax请求,但是不知道是返回的不是json还是其他的问题,就是执行不了success,代码如下:
jsp代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<br> function change(proCode){<br> alert("1 "+proCode);<br> $('#proCode').val(proCode); //让第一个下拉框保持显示选中的值<br><br> $('#cityCode').html(""); //把ci内容设为空<br><br> var cityValue = $('#cityCode');<br><br> cityValue.append('<option value="">选择CITY</option>');<br> $.ajax({ <br> url: '/GP/address.action?proCode='+proCode,<br><br> type: 'post',<br><br> //data: {},<br><br> dataType: 'json',<br> //async:false,<br> cache:false,<br> success: function(opts) {<br><br> alert(opts.length);<br> if (opts!=null && opts.length > 0) {<br><br> var html = [];<br><br> for (var i = 0; i < opts.length; i++) {<br><br> html.push('<option value="'+opts[i].cityCode+'">'+opts[i].cityName+'</option>');<br><br> }<br><br> cityValue.append(html.join(''));<br><br> }<br><br> },<br> error: function(){<br> alert("error !");<br> }<br> });<br><br> }<br> <body><br> <table><br> <tr><br> <td width="10%"> <br> 省份 <br> </td><br> <td width="30%"><br> <select name="province.proCode" id="proCode" onchange="change(this.value)"><br> <option value="0">--请选择所在省份--</option><br> <s:iterator value="#request.prolist"><br> <option value="${proCode }">${proName } ${proCode }</option><br> <a href="/s:iterator">/s:iterator</a><br> </select><br> </td><br> </tr><br> <tr><br> <td width="10%"><br> 城市 <br> </td><br> <td width="30%"><br> <select name="city.cityCode" id="cityCode"><br> <option value="0" >--city--</option><br> </select><br> </td><br> </table><br> </body><br> </html></p> <p>action代码:<br> public class AddressAction extends ActionSupport {</p> <pre><code>private static final long serialVersionUID = 1L; private IProvinceService proService; private ICityService cityService; private String proCode; private String opts; public String getProCode() { return proCode; } public void setProCode(String proCode) { this.proCode = proCode; } public String getOpts() { return opts; } public void setOpts(String opts) { this.opts = opts; } public IProvinceService getProService() { return proService; } public void setProService(IProvinceService proService) { this.proService = proService; } public ICityService getCityService() { return cityService; } public void setCityService(ICityService cityService) { this.cityService = cityService; } public String selectAddress() throws Exception { if(proCode==null)proCode=""; System.out.println("proCode="+proCode); List<City> citylist = cityService.findByProCode(proCode); List<Province> prolist = proService.searchAll(); if(proCode!=null&&proCode!="") System.out.println(citylist.get(0).getCityName()); HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("citylist", citylist); request.setAttribute("prolist", prolist); HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); PrintWriter writer = response.getWriter(); JSONArray array = JSONArray.fromObject(citylist); opts = array.toString(); writer.print(opts); return SUCCESS; } </code></pre> <p>}</p> <p>求大神赐教,谢谢!</p>