Skip to content

Instantly share code, notes, and snippets.

@ilake
Created September 6, 2012 14:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ilake/3656567 to your computer and use it in GitHub Desktop.
Save ilake/3656567 to your computer and use it in GitHub Desktop.
Add X-CSRF-Token header for ajax call to pass csrf verfication
// reference : http://stackoverflow.com/questions/7203304/warning-cant-verify-csrf-token-authenticity-rails
// : http://stackoverflow.com/questions/8511695/rails-render-json-session-lost
$.ajax({ url: 'YOUR URL HERE',
type: 'POST',
beforeSend: function(xhr) {
xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))
},
data: 'someData=' + someData,
success: function(response) {
$('#someDiv').html(response);
}
});
$.ajax({
type: 'post',
data: $(this).sortable('serialize'),
headers: {
'X-CSRF-Token': '<%= form_authenticity_token.to_s %>'
},
complete: function(request){},
url: "<%= sort_widget_images_path(@widget) %>"
})
$(document).ajaxSend(function(e, xhr, options) {
var sid = $("meta[name='csrf-token']").attr("content");
xhr.setRequestHeader("X-CSRF-Token", sid);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment