rails ajax post方式提交表单,session丢失的解决方法
rails ajax post方式提交表单,session丢失的解决办法
rails3 里面 如果开启了protect_from_forgery。那么在提交的postData中就必须加入token这个字段来通过rails的后端验证,ajax 提交的时候没有带token , 提交后session就会重置!
解决方法是
在页面加上:
<meta name="csrf-token" content="<%= form_authenticity_token %>" />
<meta name="csrf-param" content="authenticity_token" />
ajax提交之前:
beforeSend: function(xhr) {
xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
},
1 楼
该用户名已存在
2012-03-13
有<%= csrf_meta_tag %>页面上不就有<meta name="csrf-token" content="<%= form_authenticity_token %>" />
<meta name="csrf-param" content="authenticity_token" />了吗?
<meta name="csrf-param" content="authenticity_token" />了吗?