Created
February 14, 2014 06:31
-
-
Save jtmitchell/8996655 to your computer and use it in GitHub Desktop.
Sample jquery to send an ajax request when clicked
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
| <button id="mybutton" data-url="/json/endpoint/" data-value="hi there">Click me</button> | |
| <script type="application/javascript"> | |
| // like this one http://stackoverflow.com/a/8517361/2246999 | |
| $('#mybutton').live('click',function() { | |
| $.ajax({ | |
| data: { myvalue: $(this).attr('data-value'}, | |
| type: 'post', | |
| url: $(this).attr('data-url'), | |
| success: function(response, status, xhr) { | |
| alert("It Worked!"); | |
| }, | |
| error: function(response, status, error){ | |
| alert("It Failed"); | |
| } | |
| }); | |
| return false; | |
| }); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment