InvalidAuthenticityToken for JQuery despite setting authenticity token

Posted by user117046 on Stack Overflow See other posts from Stack Overflow or by user117046
Published on 2009-12-22T12:52:07Z Indexed on 2010/03/09 1:06 UTC
Read the original article Hit count: 529

Filed under:
|
|

I'm getting an InvalidAuthenticityToken despite adding in corresponding authenticity tokens in the jquery response. Is there an error in the code, or is there another, root problem? I appreciate any comments. Thanks!

Using: Rails 2.3.3, Ruby 1.8.6, Webrick, JQuery 1.3.2

layout/networks.html.haml

= token_tag
= javascript_tag "window.AUTH_TOKEN = '#{form_authenticity_token}';"

javascripts/application.js

$(document).ready(function() {

    // All non-GET requests will add the authenticity token
    // if not already present in the data packet
    $(document).ajaxSend(function(event, request, settings) {
       if (typeof(window.AUTH_TOKEN) == "undefined") return;
       // <acronym title="Internet Explorer 6">IE6</acronym> fix for http://dev.jquery.com/ticket/3155
       if (settings.type == 'GET' || settings.type == 'get') return;

       settings.data = settings.data || "";
       settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(window.AUTH_TOKEN);
     });

  ajaxLinks();
});

The rendered html has:

<input name="authenticity_token" type="hidden" value="ZaXj3ACQl+8JKtaDAUoxtSsqzEagSPyHbS25ai9qWCw=" />
<script type="text/javascript">
//<![CDATA[
window.AUTH_TOKEN = 'ZaXj3ACQl+8JKtaDAUoxtSsqzEagSPyHbS25ai9qWCw=';
//]]>
</script>

and breakpointing through, shows that window.AUTH_TOKEN has been set. Any help to resolve this would be great.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about jQuery