Skip to content

Instantly share code, notes, and snippets.

@mindscratch
Created February 22, 2012 16:50
Show Gist options
  • Select an option

  • Save mindscratch/1885993 to your computer and use it in GitHub Desktop.

Select an option

Save mindscratch/1885993 to your computer and use it in GitHub Desktop.
JavaScriptMVC Stuff
// do this once dom is ready
$(document).ajaxSend(function(e, xhr, options) {
var token = $("meta[name='csrf-token']").attr("content");
xhr.setRequestHeader("X-CSRF-Token", token);
});
// convenient way of extracting 'error' from JSON
// example JSON response:
// {error: 'something bad happened!'}
$.fn.error_from_xhr = function(jqXhr) {
if (jqXhr && jqXhr.responseText) {
var resp = $.parseJSON(jqXhr.responseText);
if (resp && resp.error) {
return resp.error;
}
}
return "unknown";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment