gpt4 book ai didi

javascript - 如何将我的 csrf token 添加到我的 $.destroy - 在 Rails 3 和 Javascript 中

转载 作者:行者123 更新时间:2023-11-28 10:20:36 25 4
gpt4 key购买 nike

所以这与另一个SO问题( Why does destroy action trigger HTTP authentication in Production in Rails 3? )有关,我认为这是该问题的核心,但不确定如何做到这一点。

显然我的 $.destroy() 没有传递必要的 CSRF token 。

但我不知道如何包含它。

这是我的 JS:

var compv = {
exists: true,
tools: {
exists: true,
csrf_param: null,
csrf_token: function() { },
clientError: function() { }
},
comments: {
exists: true,
updateView: null,
selectImage: null,
upvote: null,
edit: null,
cancelEdit:null,
downvote: null,
showVotes: null,
destroy: {
success: null,
error: null,
dialog: 'comment-destroy-dialog'
},
getUploadID: function(element) {
return $(element).parents("li").attr("data-upload-id");
}
},
steps: {
exists: true,
selectFn: {},
selectedClass: "selected-step",
selectableClass: "selectable-step",
selectedClient: {
element: null,
id: null,
stepType: "client",
ajaxSuccess: null
},
selectedProject: {
element: null,
id: null,
stepType: "project",
ajaxSuccess: null
},
selectedStage: {
element: null,
id: null,
stepType: "stage",
ajaxSuccess: null,
getID: function() {
return compv.steps.selectedStage.id;
},
displayCompare: function() {
window.open($(this).attr('data-url'), "_blank");
}
},
selectedUpload: {
element: null,
id: null,
stepType: "image",
primeUploadDisplay: null,
ajaxSuccess: null,
uploader: null,
noCloseDialog: false
}
}
};

compv.tools.csrf_param = function(){
return $('meta[name=csrf-param]').attr('content');
};

compv.tools.csrf_token = function(){
return $('meta[name=csrf-token]').attr('content');
};

这是我的$.destroy()

$.destroy({
url: element.attr('data-destroy-url'),
success: mapping.success
});

鉴于我在上述函数中获取了适当的 csrf 元数据,我如何将其传递给 .destroy() ?

我尝试添加 compv.tools.csrf_token,但收到的错误是 compv 未定义。当我执行 compv.tools.csrf_token() 时,也发生了同样的事情。

想法?

最佳答案

将 csrf 函数的结果放入一个对象中,并将该对象传递到 data 属性中。至少这在过去对我有用。

    var data = {};
data[compv.tools.csrf_param()] = compv.tools.csrf_token();
$.destroy({
url: element.attr('data-destroy-url'),
success: mapping.success,
data: data
});

关于javascript - 如何将我的 csrf token 添加到我的 $.destroy - 在 Rails 3 和 Javascript 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5739903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com