2013-10-26 76 views
0

在進行jQuery ajax調用時,我得到了HttpMediaTypeNotAcceptableExceptionHttpMediaTypeNotAcceptableException在jQuery ajax調用過程中升級到spring 3.2.4之後

我有以下配置。

在上下文我有(這是在類路徑)

<context:component-scan base-package=" group package" /> 
<mvc:annotation-driven /> 

和我在servlet的配置

<context:component-scan base-package="controller pacakges alone" /> 
<mvc:annotation-driven /> 

我已經使用spring core 3.2.4security 3.1.4

加入在pom.xml jackson-mapper-asl 1.9.13

我有一個jQuery ajax調用

$.ajax({ 
    url : "checkUser.html", 
    cache : false, 
    type : "post", 
    data : "email=" + $('#email').val(), 
    success : function(response) { 
     success callback; 
    } 
}); 

我得到這個異常:

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation 

回答

0

唉,我得到它,下面的變化作出配置內容協商春季3.2版本造成了問題,因爲內容協商通過的文件擴展名自動進行的,我在升級到3.2版本後頁面停止工作。

http://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-config-content-negotiation

我有禁用上述文件的介質類型擴展

<bean id="contentNegotiationManager" 
    class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> 
    <property name="favorPathExtension" value="false" /> 
    <property name="favorParameter" value="true" /> 
    <property name="mediaTypes"> 
     <value> 
      json=application/json 
      xml=application/xml 
     </value> 
    </property> 
</bean> 

這個職位的詳細信息。

HttpMediaTypeNotAcceptableException after upgrading to Spring 3.2

2

這是一個更好的解釋:http://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc

您可能需要設置可以接受mediaTypes像:

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> 
    <property name="mediaTypes"> 
    <map> 
    <entry key="html" value="text/html"/> 
    <entry key="json" value="application/json"/> 
    </map> 
</property> 

而且你可以簡單地發送參數,如

var url = "/checkUser/" + $('#email').val() + ".htm"; 
    $.ajax({ 
     type: "POST", 
     url: url, 
     success: function(msg){ 

在控制器中

@RequestMapping(value = "/checkUser/{email}",method = RequestMethod.POST) 
相關問題