jQuery Autocomplete不能使用Spring MVC

时间:2013-10-09 06:55:37

标签: jquery oracle spring-mvc

这是我的jsp片段

<form:form>    
<form:input path = "state"  id = "state"/>
</form:form>
<script>
$(document).ready(function() { 
    $("#state").autocomplete({         
        source: '${pageContext. request. contextPath}/getStatelist.htm'    
        }); 
});
</script>

这是应该返回状态列表的控制器代码

@RequestMapping(value = "/getStatelist.htm", method = RequestMethod.GET, headers = "Accept=*/*")
    public @ResponseBody List<String> getStateList(@RequestParam("term") String query) {

          ApplicationContext context = 
                     new ClassPathXmlApplicationContext("Beans.xml");

          MasterJDBCTemplate dao = 
                  (MasterJDBCTemplate)context.getBean("masterJDBCTemplate");
           System.out.println(query);
           System.out.println("Controller called");

           List<String> fans = dao.getStateList(query);

           return fans;
    }

运行代码并输入文本字段状态后,调用控制器并在控制台上打印出正确的结果。示例运行如下所示。

g
Controller called
select state_desc from mst_state where state_desc like 'G%'
[GUJRAT, GOA]
gu
Controller called
select state_desc from mst_state where state_desc like 'GU%'
[GUJRAT]
guj
Controller called
select state_desc from mst_state where state_desc like 'GUJ%'
[GUJRAT]

然而我无法在前端看到任何东西。我错过了什么?可能是什么原因?

1 个答案:

答案 0 :(得分:0)

根据jQuery UI文档,REST服务返回的数据应为JSON。

参考:http://api.jqueryui.com/autocomplete/#option-source

为了适应这种情况,不要从“* / *”指定Accept Header属性(即 headers =“Accept = * / *”),指定“ application / json“(即 headers =”Accept = application / json“