Skip to content

Instantly share code, notes, and snippets.

@omurphy27
Last active March 26, 2016 08:15
Show Gist options
  • Save omurphy27/c2a6b498d6d08301cab2 to your computer and use it in GitHub Desktop.
Save omurphy27/c2a6b498d6d08301cab2 to your computer and use it in GitHub Desktop.
Jquery Ajax Form Submit Example
$formUserchange.on('submit', function(e) {
e.preventDefault();
var $newUser = $(this).find('input[name=new-username]').val(),
$url = $(this).attr('action'),
$method = $(this).attr('method'),
$data = {
'new-username' : $newUser
};
$.ajax({
url: $url,
type:$method,
data: $data,
// data: $(this).serialize(), - this is another way to pass in the data
dataType: 'JSON',
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(xhr.responseText);
console.log(thrownError);
}
}).done(function(response) {
if (response.hasOwnProperty('success')) {
window.location.reload();
} else {
alert(response);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment