I'm trying to use AJAX
$.ajax({
url : urlN, // <<- "/log"
type: "post",
success : function(json) {
console.log(json);
document.getElementById('content').innerHTML = json;
}
setInterval(refreshDiv, 5000);
To update, at each 'X' times, only one part of an HTML page ( <div>
). The problem is that I could not get the contents of my file, because my controller returned the name of my view, which resulted in this:
Afterseveralattempts,IdiscoveredthatIcouldnotpasstheviewnametomycontroller,butanobjectofthefilethatIwouldliketoaccess.Underadvice,Ihaveaddedthe@ResponseBodynotationsothatthemethodreturnisautomaticallywritteninresponsetotheclient.Theproblemisthatthisway,I"lose" my view and the only return displayed is the content itself. That is, my problem was reversed ..
Mycontroller:
@RequestMapping(value="/log", method = RequestMethod.POST)
@ResponseBody
//String - View
public Log jsonLogContent(@Valid Log log, BindingResult bindingResult, Map<String, Object> model) {
if (log.getInitLine() == 0 && log.getFinalLine() == 0) {
try {
fileNumberLines(log);
log.setPathFile(logsDir + "/" + log.getFilename());
log.setContent(getLogContentByRange(0, log.getInitLine(), log.getFinalLine(), log.getPathFile()));
} catch (IOException e) {
logger.error(e.getMessage());
}
} else {
log.setContent(getLogContentByRange(0, log.getInitLine(), log.getFinalLine(), logsDir + "/" + log.getFilename()));
}
model.put("path", logsDir);
model.put("log", log);
model.put("currentPage", "logs");
model.put("root", root);
//return "log"; - View
return log; // Obj
}
My question:
Is there any way around this? Making it possible two URL's for the same controller? Or a way for the view to load first and then the object?