Spring Controller没有将列表返回到ajax成功函数

时间:2014-08-28 06:31:20

标签: jquery ajax spring spring-mvc

我想在Spring中获得自动完成功能。它的调用url但没有返回列表

我的控制器代码就是这样......

@RequestMapping(value = "purchase/ajaxcitySearchGeneral", method = RequestMethod.GET)

public@ResponseBody Map < String, List < MasterPart >> getZipcodes()

{
    List < MasterPart > zipcodes = new ArrayList < MasterPart > ();
    List < MasterPart > zipcodes = itemService.getItem();
    System.out.println("Test Value" + zipcodes);
    Map < String, List < MasterPart >> zipMap = new HashMap < String, List < MasterPart >> ();

    zipMap.put("zipcodes", zipcodes);
    System.out.println("Test Value2" + zipMap);
    return zipMap;
}

Ajax代码就是这样......

<script>
$(function () {
    $("#itemNameAuto").autocomplete({
        source: function (request, response) {

            $.ajax({
                url: 'ajaxcitySearchGeneral.htm',
                dataType: "json",
                data: {
                    maxRows: 10,
                    startsWith: request.term
                },

                success: function (data) {
                    alert(data);
                    response($.map(data.zipcodes, function (item) {
                        return {
                            label: item.name,
                            value: item.name
                        };
                    }));
                }
            });
        },

        minLength: 1,
        open: function () {
            $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
        },

        close: function () {
            $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
        }
    });
});
</script>

1 个答案:

答案 0 :(得分:-1)

您正在返回Java HashMap而不是JSON。您必须先将JAVA MAP转换为jSOn字符串。

            return new JSONObject(map).toJSONString();

结帐http://www.json.org/javadoc/org/json/JSONObject.html