jQuery-JSON填充下拉列表 [英] jQuery - JSON to populate dropdown list

查看:105
本文介绍了jQuery-JSON填充下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从需要JSON的jQuery调用中填充一个下拉列表.我在网上找到了以下代码,这是我的起点(Java和Spring 3),但是我接受其他/更好的方法:

I'm trying to populate a dropdown list from a jQuery call that requires JSON. I found on the web the following code which is my starting point (Java and Spring 3), but I accept other/better approaches:

JSP(仅显示相关代码):

The JSP (only relevant code shown):

<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控制器:

Java controller who has the requested method:

@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"下拉列表中选择一个值时,另一个未填充.使用萤火虫,我会收到此错误:

When I select a value in "parkName" dropdown the other is not populated. Using firebug I get this error:

[10:46:39.881] GET http://localhost:8084/SpringBlog/units?parkName=LA&ajax=true [HTTP/1.1 406 No Aceptable 62ms]

有什么主意吗?谢谢!

推荐答案

406 points to a Spring problem in that Spring doesn't know how to create a JSON view of the data, since $.getJSON is setting the header Accept: application/json. You should look at Spring's ContentNegotiatingViewResolver and MappingJacksonJsonView

假设您具有 <mvc:annotation-driven/> 是否已经在您的Spring Web应用程序的上下文配置中?如果是这样,则可能只是运行Spring时Jackson不在类路径上.只需手动将其添加到您的类路径中,或使用Maven,Ivy,Gradle(或其他依赖项管理/构建工具)将其添加为依赖项.

Assuming you have <mvc:annotation-driven/> in your Spring web application's context configuration already? If so then it might just be that Jackson is not on the classpath when running Spring. Just add it to you classpath manually or as a dependency if using Maven, Ivy, Gradle (or other dependency management / build tool).

这篇关于jQuery-JSON填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆