我的 406 不可接受,我一直在吃光。我尝试了很多,但它没有得到解决,请帮助我。
阿贾克斯
$('#cls').autocomplete({
source: function(request, response) {
$.ajax({
url: "classes.htm",
data: {},
contentType: "application/json; charset=utf-8",
success: function(data) {
alert(data);
response(data.names)
}
,
error:function(jqXHR, textStatus, errorThrown){
alert(textStatus);
}
})
}
});
弹簧控制器
@RequestMapping(value="/classes.htm", method = RequestMethod.GET, headers="Accept=*/*")
public @ResponseBody List<String> getClasses() {
System.out.println("ajax");
List<Classes> classes = homeService.getClasses();
List<String> clsNames = new ArrayList<String>();
for(Classes cls: classes)
{
clsNames.add(cls.getName());
}
return clsNames;
}
配置xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd>
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:annotation-config />
<context:component-scan base-package="org.app.bluez." />
<mvc:annotation-driven />
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:resources/messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jndiName"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:resources/hibernate.cfg.xml</value>
</property>
<property name="packagesToScan" value="org.app.bluez.domain" />
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
</beans>
现在我在 ajax 请求上收到此错误。我可以在这样的 XHR 响应中看到。
HTTP 状态 406 - 此请求标识的资源只能生成具有根据请求“接受”标头 () 不可接受的特征的响应。
标题
**Response Headers**
Content-Length 1070
Content-Type text/html;charset=utf-8
Date Sat, 11 May 2013 21:59:55 GMT
Server Apache-Coyote/1.1
**Request Headers**
Accept */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Connection keep-alive
Content-Type application/json; charset=utf-8
Cookie JSESSIONID=DDAC77BE351C26015C6494685E7AC225
DNT 1
Host localhost:8084
Referer http://localhost:8084/Bluez/register.htm
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:20.0) Gecko/20100101 Firefox/20.0
X-Requested-With XMLHttpRequest
jackson-mapper、jackson-core、jackson jaxrs 也被添加到项目中。有人请帮帮我,我在stackoverflow中看到过类似的问题,解决方案是降级到spring 3.1,是真的吗。我现在用的是3.2..
谢谢并恭祝安康