3
When returning the hashmap to javascript I got the following error:
The Resource identified by this request is only capable of generating Sponses with Characteristics not acceptable According to the request "Accept" headers.
Javascript validatable() function call:
<div data-role="controlgroup">
<label for="txtMesa">Informe a mesa de origem:</label>
<input type="text" name="codigo" id="txtMesa" maxlength="3" onchange="validamesa()"/>
</div>
Validate function in js file:
function validamesa(){
var mesa = document.getElementById("txtMesa").value;
if (mesa != null || mesa != "")
{
$.ajax( {
type : "GET",
url : "validarMesa.do?mesa="+mesa,
success : function(data) {
if (data.mensagem != "OK"){
alert(data.mensagem);
document.getElementById("txtMesa").value = "";
document.getElementById("txtMesa").focus();
}
}
error : function(msg) {
console.log(msg.responseText);
alert("Erro:"+msg.responseText);
}
});
}
}
Now @Requestmapping of Spring MVC:
@RequestMapping("/validarMesa.do")
public @ResponseBody
Map<String, ? extends Object> validarMesa(BigDecimal mesa,
HttpSession session, HttpServletRequest req,
HttpServletResponse resp) {
Map<String, Object> modelMap = null;
Tab_MesasDao oMesaDao = null;
try {
oMesaDao = new Tab_MesasDao();
if (oMesaDao.validar_Mesa(mesa)) {
modelMap = new HashMap<String, Object>(1);
modelMap.put("mensagem", "OK");
resp.setStatus(200);
} else {
modelMap = new HashMap<String, Object>(1);
modelMap.put("mensagem", "ERRO!" + oMesaDao.getMensagem());
resp.setStatus(200);
}
} catch (Exception ex) {
modelMap = new HashMap<String, Object>(1);
modelMap.put("mensagem", "ERRO!" + ex.getMessage());
resp.setStatus(404);
} finally {
oMesaDao = null;
}
return modelMap;
}
I’ve been having this problem for a few days now and haven’t found any forums to help me. Has anyone ever had this problem? I’m starting now with Javaweb.
The problem seems simple: your AJAX request does not say what kind of return it expects in the header
Accept
and similarly you do not specify in the method what kind of return. How will Java guess that you want JSON and not XML, for example? You need to either set the type in the method or use the attributeaccepts
in the AJAX call.– utluiz