# Jquery post authenticity\_token is not sent

**URL:** <https://www.ruby-forum.com/t/jquery-post-authenticity-token-is-not-sent/194459>\
**Category:** Rails\
**Created:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/jquery-post-authenticity-token-is-not-sent/194459 "2010-09-06T16:39:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a88e4f/32.png) [@Manos](https://www.ruby-forum.com/u/Manos)\
**Post date:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/jquery-post-authenticity-token-is-not-sent/194459/1 "2010-09-06T16:39:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Manos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a88e4f/32.png) [@Manos](https://www.ruby-forum.com/u/Manos)\
**Post date:** [September 6, 2010, 4:43pm UTC](https://www.ruby-forum.com/t/jquery-post-authenticity-token-is-not-sent/194459/2 "2010-09-06T16:43:29Z")

</div>

On 5 Óåðô, 21:25, Manos [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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?

I have found the solution according to the post  
[http://henrik.nyh.se/2008/05/rails-authenticity-token-with-jquery](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
