【问题标题】:Jquery Ajax throws Error 406--Not Acceptable in Spring MVCJquery Ajax 抛出错误 406--在 Spring MVC 中不可接受
【发布时间】:2014-06-03 20:48:38
【问题描述】:

我正在使用 Jquery AJAX 调用控制器方法。我的控制器方法被正确调用 但 ajax 返回错误 = 错误 406--不可接受。

我查看了其他帖子并遵循了所有必要的步骤。我还缺少什么吗?

我正在使用 jackson-core-asl-1.9.4.jar jackson-mapper-asl-1.9.4.jar 与 spring 3.2

我的 spring-servlet.xml 有以下

<context:component-scan  base-package="org.lacare.frc.controller" />  
<mvc:annotation-driven></mvc:annotation-driven> 
<context:annotation-config/> 

我的 JSP 有以下 ajax 调用

$.ajax({
      url: '<c:url  value="getServiceCategoriesForVisitType.html"/>' + '?visitTypeCode=' +$(this).val(),
      data: "",
      type: "GET",
      contentType: "application/json",
      dataType:"json" , 
      success: function(respContent) {

       alert(respContent); 

       },
       error:function( jqXHR,  textStatus,  errorThrown ){
            alert('error '+errorThrown);
                    }
        });  

我的Controller方法如下

   @RequestMapping(value={"/getServiceCategoriesForVisitType"},method=RequestMethod.GET,headers="Accept=*/*",produces = "application/json")
   public @ResponseBody List<String> loadreateVisitType(HttpServletRequest request,@RequestParam(value="visitTypeCode", required=true) long visitTypeCode) {    

    List<String> serviceCategories1=new ArrayList<String>();   
    serviceCategories1.add("abc"); 
    serviceCategories1.add("pqr"); 
    return serviceCategories1; 

   }

【问题讨论】:

  • 嗨,帕拉格,我面临着类似的问题。你找到解决办法了吗?

标签: jquery spring-mvc


【解决方案1】:

您只需要添加:

@ResponseStatus(HttpStatus.OK)

就在下面:

@RequestMapping(value={"/getServiceCategoriesForVisitType"},method=RequestMethod.GET,headers="Accept=*/*",produces = "application/json")  

我也面临同样的错误。这是一个答案。

【讨论】:

    【解决方案2】:

    可能是标题问题。尝试将此添加到您的$.ajax():

    headers: { 
        Accept : "application/json"
    }
    

    【讨论】:

    • 我尝试了您的解决方案,但仍然出现相同的错误 Error 406--Not Acceptable
    • 很抱歉听到这个消息。这可能会有所帮助:stackoverflow.com/questions/7336875/…
    • 尝试了所有的方法,但没有帮助。请提供一些解决方案。我真的被困住了。
    【解决方案3】:

    尝试更改您的方法以返回一个字符串,并在您的方法中将您的列表转换为 JSON 字符串。为此,您可以使用 google-gson,但鉴于我们已经在使用 spring,并且可能已经在项目中拥有这些依赖项,您可以使用 Jackson 的 ObjectMapper。像这样:

           @RequestMapping(value={"/getServiceCategoriesForVisitType"},method=RequestMethod.GET,headers="Accept=*/*",produces = "application/json")
       public @ResponseBody String loadreateVisitType(HttpServletRequest request,@RequestParam(value="visitTypeCode", required=true) long visitTypeCode)
    throws JsonProcessingException {    
        // NOTE1: change the method to return a "String"
        // NOTE2: add "throws JsonProcessingException" to the method (or catch it)
        List<String> serviceCategories1=new ArrayList<String>();   
        serviceCategories1.add("abc"); 
        serviceCategories1.add("pqr"); 
        //return serviceCategories1; 
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(serviceCategories1);
       }
    

    同时添加这两个导入:

    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    

    我很确定以上内容会为您解决问题。但只是为了确保这个答案是完整的(如果其他人提出这个关于 406 错误的问题),另一件要尝试的事情是将它添加到 @RequestMapping (你已经将它添加到你的方法中,所以也许其他人注意):

    headers="Accept=*/*",produces = "application/json"
    

    例如:

    @RequestMapping(value={"/getServiceCategoriesForVisitType"},method=RequestMethod.GET,headers="Accept=*/*",produces = "application/json")
    

    【讨论】:

      【解决方案4】:

      这里的问题是路径 value="getServiceCategoriesForVisitType.html" 在检查 Accept 标头的值之前将首先使用内容协商。对于 *.html 之类的扩展,Spring 将使用 org.springframework.web.accept.ServletPathExtensionContentNegotiationStrategy 并解析返回的可接受媒体类型是 text/html,它与 MappingJacksonHttpMessageConverter 产生的不匹配,即。 application/json,因此返回 406。

      OR
      
      Following Maven dependency could be help if there is version issue with jackson :
      
      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.5.1</version>
      </dependency>
      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.5.1</version> 
      </dependency>
      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.5.1</version>
      </dependency>
      

      【讨论】:

        猜你喜欢
        • 2016-03-08
        • 1970-01-01
        • 2013-04-26
        • 1970-01-01
        • 2013-03-18
        • 1970-01-01
        • 1970-01-01
        • 2019-04-30
        相关资源
        最近更新 更多