2 yepengjun1006 yepengjun1006 于 2013.12.04 16:18 提问

action响应ajax请求返回json类型执行不了success

我的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(&quot;1 &quot;+proCode);<br> $(&#39;#proCode&#39;).val(proCode); //让第一个下拉框保持显示选中的值<br><br> $(&#39;#cityCode&#39;).html(&quot;&quot;); //把ci内容设为空<br><br> var cityValue = $(&#39;#cityCode&#39;);<br><br> cityValue.append(&#39;<option value="">选择CITY</option>&#39;);<br> $.ajax({ <br> url: &#39;/GP/address.action?proCode=&#39;+proCode,<br><br> type: &#39;post&#39;,<br><br> //data: {},<br><br> dataType: &#39;json&#39;,<br> //async:false,<br> cache:false,<br> success: function(opts) {<br><br> alert(opts.length);<br> if (opts!=null &amp;&amp; opts.length &gt; 0) {<br><br> var html = [];<br><br> for (var i = 0; i &lt; opts.length; i++) {<br><br> html.push(&#39;<option value="'+opts[i].cityCode+'">&#39;+opts[i].cityName+&#39;</option>&#39;);<br><br> }<br><br> cityValue.append(html.join(&#39;&#39;));<br><br> }<br><br> },<br> error: function(){<br> alert(&quot;error !&quot;);<br> }<br> });<br><br> }<br> <body><br> <table><br> <tr><br> <td width="10%"> <br> 省份&nbsp;&nbsp;<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 }&nbsp;${proCode }</option><br> <a href="/s:iterator">/s:iterator</a><br> </select><br> </td><br> </tr><br> <tr><br> <td width="10%"><br> 城市&nbsp;&nbsp;<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=&quot;&quot;; System.out.println(&quot;proCode=&quot;+proCode); List&lt;City&gt; citylist = cityService.findByProCode(proCode); List&lt;Province&gt; prolist = proService.searchAll(); if(proCode!=null&amp;&amp;proCode!=&quot;&quot;) System.out.println(citylist.get(0).getCityName()); HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute(&quot;citylist&quot;, citylist); request.setAttribute(&quot;prolist&quot;, prolist); HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType(&quot;application/json&quot;); response.setCharacterEncoding(&quot;UTF-8&quot;); PrintWriter writer = response.getWriter(); JSONArray array = JSONArray.fromObject(citylist); opts = array.toString(); writer.print(opts); return SUCCESS; } </code></pre> <p>}</p> <p>求大神赐教,谢谢!</p>

1个回答

u010413669
u010413669   2013.12.05 10:28
已采纳

你的citylist这个list里面存放的对象 他的属性字段都是基本数据类型么,如果里面有一个字段类型是 一个Object类型,那么有可能执行JSONArray.formObject()这一句的时候,JSONArray里面已经出错了,你可以在这行代码处 打断点 进去看看是否 真的出现了 死循环错误

yepengjun1006
yepengjun1006 谢谢,是我返回的不是JSON数据
大约 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!