Jquery post authenticity_token is not sent

4 views
Skip to first unread message

Manos

unread,
Sep 5, 2010, 2:25:53 PM9/5/10
to Ruby on Rails: Talk
I have installed jquery-ujs driver and include jquery script in my
view.

In my view I have the following function

$(function () {
$('#alert').click(function () {
$.ajax({
type: "POST",
data: ({ lat: this.getAttribute('data-lat'), lon:
22.3908355 }),
url: '/map/setc'
//dataType: 'script'
});
return false;
})
});


but during the post authenticity_token is not send.

Authenticity token is only send with form_tag.
Is there anyway to use jQuery.ajaxSetup in order to append to the data
the authenticity token?



Manos

unread,
Sep 6, 2010, 3:53:43 AM9/6/10
to Ruby on Rails: Talk
I have found the solution according to the post
http://henrik.nyh.se/2008/05/rails-authenticity-token-with-jquery
I included in my application.js
the following

$(document).ajaxSend(function(event, request, settings) {
if (typeof(AUTH_TOKEN) == "undefined") return;
// settings.data is a serialized string like "foo=bar&baz=boink" (or
null)
settings.data = settings.data || "";
settings.data += (settings.data ? "&" : "") + "authenticity_token="
+ encodeURIComponent(AUTH_TOKEN);
});

so every time an ajax request is made authenticity_token is included
in the request
Reply all
Reply to author
Forward
0 new messages