求助jsp+ajax+json
为什么这个程序中的所在城市无法显示出来
html代码
所在省份:<s:select list="#provinceBean.selectAll()" name="province" listKey="id" listValue="name" id="province"></s:select><p>
所在城市:
jquery代码
var p = $('#province').val();
$('#province').change(function(){
p = $('#province').val();
$.ajax({
type:"post",
url:"findCities.jsp",
data:"id="+p,
dataType:"json",
success:function(data){
var cities = $('<select>');
if(data){
for(var i=0;i<data.length;i++){
cities.html("<option value = '"+data[i].cityCode+"'>"+data[i].cityName + "</option");
}
$('#province').after(cities);
}
}
});
});
jsp代码
String pid = request.getParameter("id");
if(pid == null){
pid = "34";
}
int id = Integer.parseInt(pid);
String sql = "select * from city where city.province="+id;
ResultSet rs = stmt.executeQuery(sql);
StringBuffer selectJSON = new StringBuffer("[");
while(rs.next()){
selectJSON .append("{'cityCode':'") .append(rs.getInt(1)). append("', 'cityName':'"). append(rs.getString(2)) .append("'},");
}
selectJSON.deleteCharAt(selectJSON.length() - 1);
------解决方案--------------------jsp和html都在一个页面吧,你的json数据没经过response.getWriter().println('json字符串')。