【问题标题】:jquery Ajax not working with Spring 3.2.3jquery Ajax 不适用于 Spring 3.2.3
【发布时间】:2015-04-29 14:27:39
【问题描述】:

我正在尝试学习 SPRING MVC + Jquery Ajax,但我在 ajax 成功时收到此错误,以下是我的代码配置

jQuery Ajax。

$.ajax({
        type:"GET",
        url:"" +attr.url +"",
        contentType: "application/json",
        success:function(response){
                            console.log("Response length",response.length);
                        },
                        error:function(e){
                        }
                    });

弹簧控制器

@RequestMapping(value ="/getList.htm", method= RequestMethod.GET)
    public @ResponseBody List<SomeClass> _giveList(HttpServletResponse response) throws DaException{
        List<SomeClass>someClass = iSomeClass.getData();
        return someClass;
    }

这个方法正在执行,我可以看到 someClass 的大小。 applicationContext.xml 中的 jackObjectMapper 配置

<bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper"></bean>
    <bean id="jacksonMessageChanger" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <property name="supportedMediaTypes" value="application/json"/>
    </bean>

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
    <list>
    <ref bean = "jacksonMessageChanger"/>
    </list>
    </property>
     </bean>

jackson dependency in POM.xml

    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.2.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.2.3</version>
</dependency>
<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.2.3</version>
    </dependency>
    <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
</dependency>
<dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
  </dependency>

控制器方法正在执行,但是ajax请求没有响应。所以我没有看到ajax成功函数的控制台日志。 但是我收到了这个错误

此请求标识的资源只能生成具有根据请求“接受”标头不可接受的特征的响应。 我试图通过添加 headers = "application/json" 来解决这个问题。但没有运气。 任何帮助都将是真正可观的。

【问题讨论】:

  • Jquery 是前端,Spring MVC 是后端,它与那些框架无关。你返回一个名为 shoppingBrandMasters 的东西,我似乎在你的控制器中找不到它。 .
  • 已更改返回类型。对不起那个错误。你可以抽出一些时间和谷歌 jquery ajax 和 SPRING MVC 通信
  • 我的意思是,您的问题似乎归咎于 Jquery Ajax 或 Spring MVC,因为它不适用于其他问题。实际上它正在工作,这就是您收到错误响应的原因,但错误是由于用户代码中的问题造成的。

标签: jquery spring spring-mvc


【解决方案1】:

当 spring mvc 框架无法将表示(在您的情况下为 json)从/到 java 对象转换时,您面临的问题是典型的。这可能是由于配置转换器的错误或过程中的错误而发生的。

您的情况是前者,使用 Spring 3.x 您应该删除当前的杰克逊依赖项并使用例如

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>

com.fasterxml.jackson 是用于jackson 2.x 的包,Spring 4.x 版本正在使用它。

使用您当前的代码,效果与根本没有 jackson 依赖项相同,它们负责转换 json/java 转换,这就是您收到错误的原因

【讨论】:

  • 我已经尝试过了,但它根本没有帮助。还修改了 POM 文件...现在您可以看到我正在使用的 jackson 依赖项
  • org.codehaus.jackson deps 是你应该为你的版本保留的那些,所以删除com.fasterxml.jackson,那肯定是错误的。你应该再检查一件事,你的SomeClass 是一个有效的 POJO,有一个错误的 setter/getter 会给你同样的错误
【解决方案2】:

您希望响应数据采用 JSON 格式吗?

$.ajax({
    type:"GET",
    url:"" +attr.url +"",

    // contentType is the type of data you're sending, Since you're not sending any
    // data, I assume you expect the response to be in JSON format, so you need
    // to change it to dataType: "json"
    contentType: "application/json",
    success:function(response){
        console.log("Response length",response.length);
    },
    error:function(e){
    }
});

因此,如果您希望数据列表采用 JSON 格式,您可能需要告诉您的 Contoller 它的 produces 类型。您不需要显式指定这一点,因为您已经定义了 JacksonObjectMapper,只需尝试一下即可。

@RequestMapping(value ="/getList.htm", method= RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE) 
                                                                 // ^ It's not necessary to specify like this, but just try it
public @ResponseBody List<SomeClass> _giveList(HttpServletResponse response) throws DaException{
    List<SomeClass>someClass = iSomeClass.getData();
    return someClass;
}

【讨论】:

    【解决方案3】:

    我从这篇文章中发现了问题。406 Not Acceptable: Spring 3.2 + JSON + AJAX 我用 .son 替换了 .htm 并且它起作用了。我不知道 spring 3 有什么问题。使用 .htm

    【讨论】:

      猜你喜欢
      • 2016-04-07
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多