I'm trying to run an ajax method with spring MVC, but I'm getting the error 406: "The resource identified by this request is only capable of generating responses with characteristics not acceptable According to the request" accept "headers"
Controller:
@Transactional
@Controller("user")
@SessionAttributes("user")
public class HomeController {
@Autowired
private UserDAO daoUser;
@Autowired
private EnterpriseDAO daoEnterprise;
@Autowired
private FuncDAO daoFunc;
@Autowired
private LastPeriodDAO daoLastPeriod;
@RequestMapping("/")
public String index() {
return "redirect:menu";
}
@RequestMapping(value = "/menu", method = RequestMethod.GET)
public ModelAndView menu(@ModelAttribute("user") User user, Enterprise enterprise) {
ModelAndView mav = new ModelAndView("user/menu");
Func func = daoFunc.getFunc(user);
mav.addObject("func", func);
mav.addObject("enterprise", enterprise);
mav.addObject("enterpriseList", daoEmpresa.listEnterprise(func));
return mav;
}
@RequestMapping(value = "/dynamicMenu", method = RequestMethod.POST)
public @ResponseBody List<LastPeriod> dynamicOption(@ModelAttribute("enterprise") Enterprise enterprise) {
System.out.println(enterprise.getCnpj());
List<LastPeriod> options = daoLastPeriod.getLastPeriod(enterprise);
System.out.println(options.size());
return options;
}
Request ajax:
$(document).ready(function() {
function enterpriseSelectChange() {
var enterprise= $(this).serialize();
$.ajax({
type: 'POST',
url: 'dynamicMenu',
data: enterprise,
})
.done(function(data) {
console.log("success");
console.log(data)
})
.fail(function() {
console.log("error");
});
}
$("#cnpj").change(enterpriseSelectChange);
});
Form:
<form:form modelAttribute="enterprise" commandName="enterprise" class="form-horizontal" method="POST">
<fieldset>
<legend>Olá, ${func.name}</legend>
<!-- Select enterprise -->
<div class="form-group">
<label for="enterprise">Enterprise</label>
<form:select path="cnpj" class="form-control">
<form:option value="0" label=" Select"/>
<form:options items="${enterpriseList}" itemValue="cnpj"/>
</form:select>
</div>
</form:form>
Please, anyone have any solutions ?
EDIT
Included controller and form
First thank you for your help.
I had a problem in the spring configuration, which was configured an
JsonViewResolver
,configureContentNegotiation
,contentNegotiatingViewResolver
. I think I had to specify a return on the controller.As just I need a JSON disregarded those settings and is now working.
Personal , first thank you for your help.
I had a problem in the spring configuration, which was configured an JsonViewResolver , configureContentNegotiation , contentNegotiatingViewResolver . I think I had to specify a return on the controller.
As just I need a JSON disregarded those settings and is now working.
I also changed my method because it does not need to receive an object , only one id. Therefore it looked like this:
Request ajax: