我使用Rails 3和javascript发出POST请求,因此我需要一个真实性令牌。但是,令牌Rails创建包含空格的保持,我不能在我的javascript URL字符串中。以下是我现在的日志中的样子:
Started POST "/cart/update?authenticity_token=fjJJQc2gKBenzzAAqdvhprJxH2tnhYkyuZ9F+l+GFns=" for 127.0.0.1 at 2013-05-23 11:55:42 -0400
Processing by CartController#update as HTML
Parameters: {"gift_card_amount"=>"undefined", "case_quantity"=>"", "bottle_quantity"=>"", "product_history_id"=>"1052981", "authenticity_token"=>"fjJJQc2gKBenzzAAqdvhprJxH2tnhYkyuZ9F l GFns="}
WARNING: Can't verify CSRF token authenticity
这是我的HTML:
<a href="javascript: void(0)" class="button-yellow">Add to Cart</a>
<%= hidden_field_tag form_authenticity_token %>
<script>
window._token = '<%= form_authenticity_token %>';
</script>
我的javascript:
$.post('/cart/update?authenticity_token=' + encodeURIComponent(window._token), ...
注意我尝试使用encodeURIComponent
,但这似乎不起作用。但是,问题仍然存在,我觉得它是Rails而不是javascript。如何将Rails标记更改为不包含空格?像的gsub
+
就像在javascript中一样吗?