1

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:

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 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;
    }

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]

Any idea? Thanks!!

11
  • can you show the actual request done to the server by the script? and if you use "by hand" the url to retrieve the data, do you receive what you expect? Commented Apr 23, 2013 at 9:20
  • 1
    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 Commented Apr 23, 2013 at 9:20
  • @ShinTakezou: What do you mean by "by hand"? Writing it directly into the browser? I've tried that but it retrieves an error (I'm probably not doing it properly....) Could you show me an example, please? Commented Apr 23, 2013 at 9:40
  • @andyb: In fact, I haven't done any JSON configuration, though I saw it was necessary adding some Jackson dependencies. However I don't know where to add them... Commented Apr 23, 2013 at 9:42
  • 1
    Hey, now it works!! Thank you!! I just added the Jackson .jars to my project and works perfectly. Commented Apr 23, 2013 at 11:00

1 Answer 1

1

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

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.