我的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>
你的citylist這個list裡面存放的對象 他的屬性字段都是基本數據類型麼,如果裡面有一個字段類型是 一個Object類型,那麼有可能執行JSONArray.formObject()這一句的時候,JSONArray裡面已經出錯了,你可以在這行代碼處 打斷點 進去看看是否 真的出現了 死循環錯誤