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
406 means "not acceptable". Try to add header content-type when sending the request:
$.ajax({
type: 'POST',
headers:{
'Content-type:application/x-www-form-urlencoded'
},
url: 'dynamicMenu',
data: enterprise,
})
.done(function(data) {
console.log("success");
console.log(data)
})
.fail(function() {
console.log("error");
});
application/x-www-form-urlencoded
Essentially, the error message is saying that the server cannot generate a response type that is acceptable to your AJAX call. The server is expecting your "Accept" header to contain a type that it can generate response in.
Try setting the "Accept" header in your AJAX call:
$.ajax({
type: 'POST',
headers:{
'Accept:application/json'
},
...
})
Note the difference between the headers
The other comment asks you to set your content-type but the error message you posted seems to indicate that you need to set the "Accept" header.
So here is your problem,
You need to return jsonObject
but actually you are returning the List of LastPeriod Object i.e. List<LastPeriod>
.
So you need to change in method,
@RequestMapping(value = "/dynamicMenu", method = RequestMethod.POST)
public @ResponseBody List<LastPeriod> dynamicOption(@ModelAttribute("enterprise") Enterprise enterprise) {
List<LastPeriod> options = daoLastPeriod.getLastPeriod(enterprise);
JSONArray jsonArray = new JSONArray();
for(LastPeriod lastPeriod: options){
JSONObject jsonObject = new JSONObject();
//Here put data in jsonObject from lastPeriod like
jsonObject.put("name", "populate field from lastPeriod");
jsonArray.add(jsonObject);
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("data", jsonArray);
return jsonObject.toJSONString();
}
Now you will get ajax response as array like.
$.ajax({
type: 'POST',
url: 'dynamicMenu',
data: enterprise,
success: function(resp){
var parseJson = JSON.parse(resp);
console.log(parseJson.data);
}
});
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:
@RequestMapping(value = "/dynamicMenu/{cnpj}", method = RequestMethod.GET)
public @ResponseBody List<UltimoPeriodoAberto> DynamicOptions(@PathVariable final String cnpj) {
return daoLastPeriod.getLastPeriod(new Enterprise(cnpj));
}
Request ajax:
var cnpj = $(this).val();
$.ajax({
type: 'get',
url: 'http://localhost:8080/myincome/dynamicMenu/' + cnpj,
})
.done(function(data) {
console.log("success");
console.log(data[0]);
for ( var i = 0; i < data.length; i++) {
console.log(data[i].description);
}
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.log("The following error occured: " + textStatus, errorThrown);
});