首页 > 解决方案 > 如何在 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: ...

无论如何,当检查元素时,我的表单中没有输入元素。我错过了什么?

标签: ruby-on-railsajax

解决方案


推荐阅读