如何添加真实性令牌? [英] How do i add the authenticity token?
本文介绍了如何添加真实性令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我最近改用Google关闭了一个新项目.我在将真实性令牌添加到ajax调用的标头中时遇到麻烦.我该怎么办?
I recently switched to Google closure for a new project. I am having trouble adding the authenticity token to the headers in a ajax call. How do i go about it?
我的Ajax代码段(使用goog.net.XhrIo类):
My Ajax snippet (using goog.net.XhrIo class):
var initialHTMLContent = superField[i].getCleanContents();
var data = goog.Uri.QueryData.createFromMap(new goog.structs.Map({
body: initialHTMLContent
}));
goog.net.XhrIo.send('/blogs/create', function(e) {
var xhr = /** @type {goog.net.XhrIo} */ (e.target);
alert(xhr.getResponseXml());
}, 'POST', data.toString(), {
'Accept' : 'text/xml'
});
在后端使用滑轨.
更新:
日志:
Processing BlogsController#create (for 127.0.0.1 at 2010-06-29 20:18:46) [PUT]
Parameters: {"authenticity_token"=>""}
ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
Rendered rescues/_trace (272.4ms)
Rendered rescues/_request_and_response (1.2ms)
Rendering rescues/layout (unprocessable_entity)
推荐答案
在Rails视图(.html.erb文件)中的某个位置,您可以设置js变量,如下所示:
Somewhere in a rails view (.html.erb file) you can set a js variable like this:
window._token = '<%= form_authenticity_token %>';
然后将其添加到您的通话中:
And then append it in your call:
goog.net.XhrIo.send('/blogs/create?authenticity_token=' + window._token, function(e) {
var xhr = /** @type {goog.net.XhrIo} */ (e.target);
alert(xhr.getResponseXml());
}, 'POST', data.toString(), {
'Accept' : 'text/xml'
});
这篇关于如何添加真实性令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文