1
我想从需要JSON的jQuery调用填充下拉列表。我在网上找到下面的代码是我的出发点(Java和春季3),但我接受其他/更好的方法:jQuery - JSON来填充下拉列表
的JSP(仅显示相关的代码):
<script language="JavaScript">
$(document).ready(function() {
$('#parkName').change(
function(){
alert($(this).val());
$.getJSON('${findUnitsURL}', {
parkName : $(this).val(),
ajax : 'true'
}, function(data) {
var html = '<option value="">City</option>';
var len = data.length;
for (var i = 0; i < len; i++) {
html += '<option value="' + data[i].name + '">'
+ data[i].name + '</option>';
}
html += '</option>';
$('#parkUnitTitleAersa').html(html);
});
});
});
</script>
<div id="content">
<form:form method="post" action="mainForm" commandName="mainForm">
<form:select id="parkName" path="parkName">
<form:option value="NONE" label="--- Select ---" />
<form:options items="${parkList}" />
</form:select>
<form:select id="parkUnitTitleAersa" path="parkUnitTitleAersa">
<form:option value="NONE" label="--- Select ---" />
<form:options items="${parkUnitList}" />
</form:select>
<p class="submit"><input type="submit" name="commit" value="Login"></p>
</form:form>
</div>
的Java控制器谁有请求的方法:
@RequestMapping(value = "units", method = RequestMethod.GET)
public @ResponseBody List<String> unitsForPark(@RequestParam(value = "parkName", required = true) String parkName) {
List<String> l = new ArrayList<String>();
l.add("AA01");
l.add("AA02");
l.add("LA03");
l.add("SG04");
return l;
}
当我在“parkName”下拉列表中选择一个值时,另一个未填充。使用萤火虫我得到这个错误:
[10:46:39.881] GET http://localhost:8084/SpringBlog/units?parkName=LA&ajax=true [HTTP/1.1 406 No Aceptable 62ms]
任何想法?谢谢!!
你可以通过脚本显示服务器完成的实际请求吗?如果您使用“手动”url来检索数据,您是否收到您的期望? – ShinTakezou 2013-04-23 09:20:00
[''406'](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7)指向Spring问题,因为Spring不知道如何创建'JSON'数据视图,因为'$ .getJSON'设置头部'Accept:application/json'。你应该看看Spring的[ContentNegotiatingViewResolver](http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/servlet/view/ContentNegotiatingViewResolver.html)和[MappingJacksonJsonView]( http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/servlet/view/json/MappingJacksonJsonView.html) – andyb 2013-04-23 09:20:39
@ShinTakezou:你是什么意思的“手工“?直接写入浏览器?我已经尝试过,但它检索到一个错误(我可能没有正确地做它....)你能告诉我一个例子吗? – Hauri 2013-04-23 09:40:17