2013-10-09 113 views
0

这里是我的jsp片段jQuery的自动完成功能无法使用Spring MVC工作

<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] 

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

回答

0

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

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

为了适应的是,而不是指定来自Accept报头属性 “*/*”(即头= “接受= */*”)指定“应用程序/ JSON”(即headers =“Accept = application/json”

相关问题