Created
January 31, 2014 18:13
-
-
Save airtwo/8739011 to your computer and use it in GitHub Desktop.
AJAX JSON request (js)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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