Created
April 24, 2014 15:06
-
-
Save nealfennimore/11258112 to your computer and use it in GitHub Desktop.
Kittens AJAX
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
$('form').submit( function(e){ | |
e.preventDefault(); | |
var kitten = $(this).serialize(); // sends as string | |
$.ajax({ | |
type: "POST", | |
url: "/kittens/new", | |
data: kitten | |
}) | |
.done( function(data) { | |
var id = data.kitten_id | |
$('#kittens').prepend('<div>'); | |
$('#kittens div:first').attr('id', 'kitten '+id); | |
$('#kitten_'+id).append('<p><strong>'+data.name+'</strong></p>'); | |
$('#kitten_'+id).append('<p><em>'+data.bredd+'</em></p>'); | |
}) | |
.fail( function() { | |
alert("Request failed."); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment