jQuery的自动完成抛出406不可接受的错误 [英] Jquery AutoComplete Throws 406 Not Acceptable error

查看:139
本文介绍了jQuery的自动完成抛出406不可接受的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过下面这个例子来创建我的春天MVC应用程序自动完成框 http://www.mkyong.com/spring-mvc/弹簧MVC-的jQuery自动完成,例如/ 每当我尝试获取的细节它显示在浏览器中出现以下错误 NetworkError:406无法接受这是我的控制器

  @RequestMapping(值=/searchTags.htm,方法= RequestMethod.GET)
    公共@ResponseBody
    名单< SolrResult> getTags(@RequestParam字符串标记名)抛出的Throwable

        返回fetchData(标签名);

    }
 

下面是我的POJO类

 公共类SolrResult {
    私人字符串ID;
    私人字符串标签;
    公共字符串的getId(){
        返回ID;
    }
    公共无效SETID(字符串ID){
        this.id = ID;
    }
    公共字符串getLabel(){
        返回标签;
    }
    公共无效setLabel(字符串标签){
        this.label =标签;
    }
    @覆盖
    公共字符串的toString(){
        返回SolrResult [ID =+编号+,标签=+标签+];
    }

}
 

这是我的JavaScript

 <脚本>
    $(文件)。就绪(函数(){

        $('#inputText的')。自动完成({
            的serviceUrl:$ {pageContext.request.contextPath} /searchTags.htm',
            paramName配置:变量名
            定界符:,
            transformResult:功能(响应){
                返回 {
                    建议:$ .MAP($ parseJSON(响应),功能(项目){
                        返回{值:item.label,数据:item.id};
                    })
                };
            }
        });


    });
    < / SCRIPT>
 

解决方案

我有同样的问题,但我固定使用GSON(),以这种方式:

  @RequestMapping(值=/ getCustomer,方法= RequestMethod.GET)
@ResponseBody字符串getCustomer(@RequestParam查询字符串){

    CustomerDao customerDao =(CustomerDao)ApplicationProperty.getApplicationContext()的getBean(CustomerDao);
    GSON GSON =新GSON();
    返回gson.toJson(customerDao.getCustomer(查询,USERPROFILE));

}
 

I am trying to create an auto complete box in my spring mvc application by following this example http://www.mkyong.com/spring-mvc/spring-mvc-jquery-autocomplete-example/ Whenever I try to fetch the details it shows the following error in my browser NetworkError: 406 Not Acceptable Here is my controller

@RequestMapping(value = "/searchTags.htm", method = RequestMethod.GET)
    public @ResponseBody
    List<SolrResult> getTags(@RequestParam String tagName) throws Throwable {

        return fetchData(tagName);

    }

Here is my POJO class

public class SolrResult {
    private String id;
    private String label;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getLabel() {
        return label;
    }
    public void setLabel(String label) {
        this.label = label;
    }
    @Override
    public String toString() {
        return "SolrResult [id=" + id + ", label=" + label + "]";
    }

}

This is my javascript

<script>
    $(document).ready(function() {

        $('#inputText').autocomplete({
            serviceUrl: '${pageContext.request.contextPath}/searchTags.htm',
            paramName: "tagName",
            delimiter: ","
            transformResult: function(response) {
                return {
                    suggestions: $.map($.parseJSON(response), function(item) {
                        return { value: item.label, data: item.id };
                    }) 
                };  
            }  
        });


    });
    </script>

解决方案

I had the same issue but I fixed using Gson(), in this way:

@RequestMapping(value = "/getCustomer", method = RequestMethod.GET)
@ResponseBody String getCustomer(@RequestParam String query) {

    CustomerDao customerDao = (CustomerDao) ApplicationProperty.getApplicationContext().getBean("CustomerDao");
    Gson gson = new Gson();
    return gson.toJson(customerDao.getCustomer(query, userProfile));

}

这篇关于jQuery的自动完成抛出406不可接受的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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