action响应ajax请求返回json类型执行不了success
我的action能响应ajax请求,但是不知道是返回的不是json还是其他的问题,就是执行不了success,代码如下:jsp代码:<scripttype="text/j...
我的action能响应ajax请求,但是不知道是返回的不是json还是其他的问题,就是执行不了success,代码如下:
jsp代码:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript">
function change(proCode){
alert("1 "+proCode);
$('#proCode').val(proCode); //让第一个下拉框保持显示选中的值
$('#cityCode').html(""); //把ci内容设为空
var cityValue = $('#cityCode');
cityValue.append('<option value="">选择CITY</option>');
$.ajax({
url: '/GP/address.action?proCode='+proCode,
type: 'post',
//data: {},
dataType: 'json',
//async:false,
cache:false,
success: function(opts) {
alert(opts.length);
if (opts!=null && opts.length > 0) {
var html = [];
for (var i = 0; i < opts.length; i++) {
html.push('<option value="'+opts[i].cityCode+'">'+opts[i].cityName+'</option>');
}
cityValue.append(html.join(''));
}
},
error: function(){
alert("error !");
}
});
}
<select name="province.proCode" id="proCode" onchange="change(this.value)">
<option value="0">--请选择所在省份--</option>
<s:iterator value="#request.prolist">
<option value="${proCode }">${proName } ${proCode }</option>
</s:iterator>
</select>
</td>
</tr>
<select name="city.cityCode" id="cityCode">
<option value="0" >--city--</option>
</select>
action代码:
public class AddressAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private IProvinceService proService;
private ICityService cityService;
private String proCode;
private String opts;
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;
}
} 展开
jsp代码:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript">
function change(proCode){
alert("1 "+proCode);
$('#proCode').val(proCode); //让第一个下拉框保持显示选中的值
$('#cityCode').html(""); //把ci内容设为空
var cityValue = $('#cityCode');
cityValue.append('<option value="">选择CITY</option>');
$.ajax({
url: '/GP/address.action?proCode='+proCode,
type: 'post',
//data: {},
dataType: 'json',
//async:false,
cache:false,
success: function(opts) {
alert(opts.length);
if (opts!=null && opts.length > 0) {
var html = [];
for (var i = 0; i < opts.length; i++) {
html.push('<option value="'+opts[i].cityCode+'">'+opts[i].cityName+'</option>');
}
cityValue.append(html.join(''));
}
},
error: function(){
alert("error !");
}
});
}
<select name="province.proCode" id="proCode" onchange="change(this.value)">
<option value="0">--请选择所在省份--</option>
<s:iterator value="#request.prolist">
<option value="${proCode }">${proName } ${proCode }</option>
</s:iterator>
</select>
</td>
</tr>
<select name="city.cityCode" id="cityCode">
<option value="0" >--city--</option>
</select>
action代码:
public class AddressAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private IProvinceService proService;
private ICityService cityService;
private String proCode;
private String opts;
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;
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询