尽管在 jquery 响应中添加了相应的真实性令牌,但我得到了 InvalidAuthenticityToken。代码中是否存在错误,或者是否存在另一个根本问题?我感谢任何评论。谢谢!
使用:Rails 2.3.3、Ruby 1.8.6、Webrick、JQuery 1.3.2
布局/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();
});
呈现的 html 具有:
<input name="authenticity_token" type="hidden" value="ZaXj3ACQl+8JKtaDAUoxtSsqzEagSPyHbS25ai9qWCw=" />
<script type="text/javascript">
//<![CDATA[
window.AUTH_TOKEN = 'ZaXj3ACQl+8JKtaDAUoxtSsqzEagSPyHbS25ai9qWCw=';
//]]>
</script>
并通过断点显示 window.AUTH_TOKEN 已设置。解决这个问题的任何帮助都会很棒。