Created
April 1, 2015 06:15
-
-
Save data-doge/26f143fe6ce5ddb61077 to your computer and use it in GitHub Desktop.
ajax-example javascript
This file contains hidden or 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
| $(document).ready(function() { | |
| $('#meow').click(function(e) { | |
| e.preventDefault(); | |
| console.log("WE ARE NOT BEING REDIRECTED, yes"); | |
| $.ajax({ | |
| type : 'GET', | |
| url : '/cats', | |
| success : function(res) { | |
| console.log("EVERYTHING WORKED"); | |
| $meowbox = $('#meow-box'); | |
| $meowbox.empty(); | |
| var cats = res.cats; | |
| var $ul = $('<ul></ul>'); | |
| $meowbox.append($ul); | |
| for (var i = 0; i < cats.length; i++) { | |
| var currentCat = cats[i]; | |
| var name = currentCat.name; | |
| var product = currentCat.product; | |
| var $li = $('<li></li>'); | |
| $ul.append($li); | |
| $li.append('<h2>' + name + '</h2>'); | |
| $li.append('<h3>' + product + '</h3>'); | |
| } | |
| }, | |
| error : function() { | |
| console.log("EVERYTHING IS BROKEN AND IM HUNGRY"); | |
| } | |
| }); | |
| }); | |
| $('#cat-form').on('submit', function(e) { | |
| e.preventDefault(); | |
| $.ajax({ | |
| type: 'POST', | |
| url: '/cats', | |
| data: $(this).serialize(), | |
| success: function() { | |
| var $successMessage = $('<p>CAT ADDED SCCUCESFULLY</p>'); | |
| $('body').append($successMessage); | |
| }, | |
| error: function() { | |
| $('body').html("<img src='http://www.buckybox.com/images/team-joshua-63101086.jpg'>"); | |
| } | |
| }); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment