Skip to content

Instantly share code, notes, and snippets.

@airtwo
Created January 31, 2014 18:13
Show Gist options
  • Save airtwo/8739011 to your computer and use it in GitHub Desktop.
Save airtwo/8739011 to your computer and use it in GitHub Desktop.
AJAX JSON request (js)
$(function() {
// On unfocus, check the value of the username field, and
// store that in a variable.
$("#username").focusout(function() {
var usernameInput = $(this).val();
// We want to take that variable and send the variable to
// the "server" and get back a JSON hash
$.getJSON(
'/usernames.json',
usernameInput,
function(returndata, coolstatus ) {
var fieldset = $('#username').closest('fieldset');
if (returndata['valid'] === '0') {
fieldset.append("Invalid Username");
} else {
// Then, we want to check that hash...if its valid, do nothing,
// else, indicate that to the user (that username is taken, or something)
fieldset.append("Great Username");
}
}
);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment