I have a simple JQuery function which calls my Spring MVC controller:
function createDatabaseRecord() {
if (isFormValid()) {
$.getJSON(
"${pageContext.servletContext.contextPath}${databaseConfigUrl}",
{name:$('#idName').val(),
databaseName:$('#idDatabaseName').val(),
hostName:$('#idHostName').val(),
username:$('#idUsername').val(),
password:$('#idPassword').val()},
function (data) {
var html = '<span>' + data + '</span>';
$('#idMessage').html(html);
}
);
}
}
The function is invoked when I click the button, everything is fine. The problem is that this line: $('#idMessage').html(html); does not work as expected. I have a <div> element inside the same JSP page.
<div id="idMessage">
</div>
My Spring controller method which is invoked by that function:
@RequestMapping(value = DATABASE_CONFIG_RECORD_MAPPING, method = RequestMethod.GET)
public
@ResponseBody
String createDatabaseConfiguration(@RequestParam(value = "name", required = true) String name,
@RequestParam(value = "databaseName", required = true) String databaseName,
@RequestParam(value = "hostName", required = true) String hostName,
@RequestParam(value = "username", required = true) String username,
@RequestParam(value = "password", required = true) String password) {
try {
DatabaseConfig config = new DatabaseConfig();
config.setName(name);
config.setDatabaseName(databaseName);
config.setHostName(hostName);
config.setUsername(username);
config.setPassword(password);
configService.save(config);
} catch (Exception ex) {
LOGGER.error("Exception while create database configuration record.", ex);
return "Error occurred while creating database configuration record: " + ex.getMessage();
}
return "Database configuration record successfully created.";
}
I can see in the FireBug that response came to me:
. This is the message: Database configuration record successfully created. I expect to be show in that <div> element, but it is not displayed. Does someone know where might be the problem? If you need some more code, please ask.
Your response is not in JSON format.
Try this snippet in your controller:
And use it in javascript like this: