ruby-on-rails - 如何在 AJAX 请求中附加输入元素?
问题描述
我想<%= csrf_meta_tag %>
通过 AJAX 请求在 application.html.erb 中附加真实性令牌。
#_settings.html.erb
<form id="settings" method="post" action="/settings/save">
#The input element should go here
</form>
这是我在 AJAX 请求 js 文件中尝试做的事情:
function settings(){
var AUTH_TOKEN = jQuery('meta[name=csrf-token]').attr('content');
form = jQuery('<form/>');
form.attr('method', 'post');
form.append(jQuery('<input>').attr({
type: 'hidden',
id: 'authenticity_token',
name: 'authenticity_token',
value: AUTH_TOKEN
}));
form.submit();
jQuery.ajax({
type: 'POST',
url: '/settings/save',
success: ...
无论如何,当检查元素时,我的表单中没有输入元素。我错过了什么?
解决方案
推荐阅读
- visual-studio - How to copy IIS configuration to another machine
- python - 让用户从视图/表单中建模
- r - 如何从列中选择数据并在 R 中生成 nw 列
- ruby-on-rails - 强参数中允许的提交的数组参数消失
- node.js - 未捕获的 ReferenceError:要求未在 Electron 中定义
- android - 从 Firebase 导入数据时出现以下错误:在 null 上调用了方法“[]”
- swift - 具有最大值的 SwiftUI 自定义滑块
- flutter - 如何设置与其下一个容器相关的动态容器高度?
- r - R调用非唯一值重复
- jenkins - 詹金斯。使用下拉按钮选择不同的选项