У меня возникли проблемы с простым запросом POST ajax. Когда я вызываю doAjax на загрузку страницы, он возвращает мне erorr 406 неприемлемым. У меня правильные зависимости.Ошибка Spring MVC Ajax 406
Простая модель
package proj1.models;
public class Popravilo{
public String id = null;
public String name = null;
}
Контроллер
@RequestMapping(value = "car_list", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody Popravilo showPopravila(){
Popravilo p = new Popravilo();
p.id="1";
p.name ="p1";
return p;
}
Ajax
$(document).ready(function(){
doAjaxPost();
});
function doAjaxPost() {
$.ajax({
type: "POST",
url: "car_list.htm",
success: function(response) {
console.log(response);
}
});
}
грузоотправитель сервлет
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:resources mapping="WEB-INF/resources/*" location="resources/" cache-period="31556926"/>
<mvc:annotation-driven />
<context:component-scan base-package="proj1.controllers"></context:component-scan>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>proj1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>*.htm</url-pattern>
<url-pattern>*.json</url-pattern>
<url-pattern>*.xml</url-pattern>
</servlet-mapping>
</web-app>
Вы должны указать заголовок 'Accept' в Ajax вызова как' приложения/json'. – 11thdimension
hm, я добавил, что для ajax, но, пожалуйста, посмотрите заголовки, я разместил изображение сейчас – Ales
Сервер возвращает тип содержимого 'text/html', это неверно, так как вы ожидаете' application/json' – 11thdimension