我在使用下面的JQuery,JSP和Controller来返回在应用程序JSP页面上更改国家下拉菜单时的城市列表,但我认为jQuery中的dataType,成功参数不正确因为我没有收回清单。那么有人可以告诉我我在做什么错了,以及如何通过更改国家/地区下拉菜单来获得城市列表以将其添加到城市下拉菜单中?使用JQuery返回一个列表
感谢您的时间
<script type="text/javascript">
function loadCity(){
var countryData="countryId="+$(country).val();
$.ajax({
type: "POST",
url: "LoadCities.htm",
data:countryData,
dataType: "javascript",
success: function(cities){
loadCities(eval(cities))
}
});
}
function loadCities(citiesLst){
clearCitiesDD();
for(var i=0;i<citiesLst.length;i++){
var city=citiesLst[i];
appendCity(city.id,city.value);
}
}
function clearCitiesDD(){
$('#cityDD').find('option').remove();
}
function appendCity(id,cityName){
$('#cityDD').append($("<option id='"+id+"'>" + cityName + "</option>"));
}
}
</script>
,并在我的应用程序控制器我有以下代码:
@RequestMapping(value = "/LoadCities.htm", method = RequestMethod.GET)
public @ResponseBody
List<City> getCityByCountryID(HttpServletRequest request, HttpServletResponse response)
throws Exception {
List<City> list=
icitys.getCitiesByCountry(Long.parseLong(request.getParameter("countryID")));
return list;
}
,并在JSP文件我有以下的城市和国家下拉菜单:
<tr>
<td align="right">Country</td>
<td align="left">
<select id ="country" name="country" onchange="loadCity()">
<option value="0">--select--</option>
<c:forEach var="countrylst" items="${countriesList}">
<option value="${countrylst.id}">${countrylst.countryName}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td align="right">City</td>
<td align="left">
<select id="cityDD" name="cityDD">
<option value="0">--select--</option>
</select>
</td>
</tr>
您是否在控制台中检查了响应,以确保它的格式符合您的预期? –
@RoryMcCrossan检查了firebug中的响应,但没有得到任何东西:(这是否意味着我的代码是正确的?! – MChan
不,这意味着您的JSP中的getCityByCountryID不返回任何内容 –