Skip to content

Instantly share code, notes, and snippets.

@SriniBlog
Created December 25, 2015 15:11
Show Gist options
  • Save SriniBlog/645b1db84f732b4d3708 to your computer and use it in GitHub Desktop.
Save SriniBlog/645b1db84f732b4d3708 to your computer and use it in GitHub Desktop.
This is a code snippet of AJAX to call servlet and get the response from the server. Example is shown with JQuery.
$(document).ready(function () {
//Stops the submit request
$("#frmSomeForm").submit(function(e){
e.preventDefault();
});
//checks for the button click event for New
$("#btnSubmit").click(function(e){
dataString = "somefield=" + $("#frmSomeForm #someField").val();
//make the AJAX request, dataType is set to json
//meaning we are expecting JSON data in response from the server
$.ajax({
type: "POST",
url: "ServletController?action=execute",
data: dataString,
cache: false,
dataType: "json",
//if received a response from the server
success: function( data, textStatus, jqXHR) {
console.log("Response from ServletController " + textStatus);
},
//If there was no resonse from the server
error: function(jqXHR, textStatus, errorThrown){
console.log("Something really bad happened " + textStatus);
},
//capture the request before it was sent to server
beforeSend: function(jqXHR, settings){
//disable the submit button until we get the response
},
//this is called after the response or error functions are finished
//so that we can take some action
complete: function(jqXHR, textStatus){
//enable the submit button
}
}); //End of Ajax Call
}); // End of Submit button function
} // End of JQuery
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment