I am using grails 2.5.2 version. I have a form submit event by ajax call where sometimes form is submitted multiple times. I have goggled for it and see about grails withForm topic. I tried to implement it but it stops the request for the first time and show invalid token message. How can I use useToken for withForm or other way to stop multiple time form submit via ajax call in grails.
My code attempts are below.
My ajax call:
jQuery.ajax({
type: 'POST',
dataType:'JSON',
data: $("#createFormModal").serialize(),
url: "${g.createLink(controller: 'studentCollection', action: 'save')}",
success: function (data) {
if(data.isError==true){
showAlertModal(data.message);
}else {
$('#createModal').modal('hide');
$('#list-table').DataTable().ajax.reload();
showSuccessMsg(data.message);
$("#createFormModal").find("input[type=text], textarea").val("");
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
}
});
My controller method:
def save() {
LinkedHashMap result = new LinkedHashMap()
String outPut
withForm {
result = studentCollectionService.saveCollection(params)
}.invalidToken {
println("### invalidate Token")
result.put(CommonUtils.IS_ERROR, Boolean.TRUE)
result.put(CommonUtils.MESSAGE, "Please press the button once")
}
outPut = result as JSON
render outPut
return
}