Skip to content

Instantly share code, notes, and snippets.

@jtmitchell
Created February 14, 2014 06:31
Show Gist options
  • Select an option

  • Save jtmitchell/8996655 to your computer and use it in GitHub Desktop.

Select an option

Save jtmitchell/8996655 to your computer and use it in GitHub Desktop.
Sample jquery to send an ajax request when clicked
<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