Reputation: 89
Java 8 / Spring 4 / Maven / jQuery
So I crafted a Spring MVC webapp whose index.jsp uses an ajax call
--- index.jsp snippet ----
function popupRuleDeck_update_submit() {
var formJsonStr = $('#form_popupRuleDeck_update').serialize();
$.ajax({
url: '${pageContext.request.contextPath}/ruleDeck_update',
type: "post",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: formJsonStr,
success: function( response) {
$('#ruleDeckTable').row( this).data( response).draw( false);
return true;
},
error: function( response) {
console.log( response);
alert( "ruleDeck update error: " + response.message)
return false;
}
});
}
to a method in my controller
@RequestMapping( value="/ruleDeck_update", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody AjaxResponseBody ruleDeckUpdateHandler( @RequestBody RuleDeck formRuleDeck)
{
AjaxResponseBody result = new AjaxResponseBody( false, "FAILED");
RuleDeck ruleDeck = null;
try {
ruleDeck = tryThisService.findOneRuleDeck( formRuleDeck.getId());
ruleDeck.setRuleDeckId( formRuleDeck.getRuleDeckId());
ruleDeck.setRevision( formRuleDeck.getRevision());
ruleDeck.setName( formRuleDeck.getName());
ruleDeck.setDeckType( formRuleDeck.getDeckType());
ruleDeck.setFileLocation( formRuleDeck.getFileLocation());
ruleDeck = tryThisService.updateOneRuleDeck( ruleDeck);
result.setStatus( true);
result.setMessage("update successful");
} catch (JsonProcessingException e) {
result.setMessage( "JPE:" + e.getMessage());
} catch ( Exception e) {
result.setMessage( "Exception:" + e.getMessage());
}
return result; //should be implicitly converted into json format and send back to the request.
}
where
public class AjaxResponseBody {
@JsonView
private boolean status;
@JsonView
private String message;
// assorted constructors, getters, setters omitted...
}
I do have Jackson databinding dependency in my pom.xml
I have not explicitly configured any Http/Json/Xml message converters.
I've used this page as a template.
The ajax call sends the JSON to the method, which does find and update an existing object, which gets saved in the database, so far so good. The method executes without errors. But the ajax call always hits the error function and response seems to be undefined, as if the method doesn't actually return a serialized AjaxResponseBody object. Obviously, the goal here is to make sure the success function gets hit when the method throws no errors and to hit the error function and extract the error message when the method throws an error.
Or is there some better pattern I should be using?
TIA,
code_warrior
Upvotes: 0
Views: 362
Reputation: 1042
Could it be that you are using jQuery >= 3.0 and you should use methods done()
instead of success()
and fail()
instead of error()
?
In jQuery documentation :
Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are removed as of jQuery 3.0. You can use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.
Upvotes: 1