Laravel Bootstrap-Modal TokenMismatch

时间:2016-05-02 13:05:04

标签: jquery laravel modal-dialog token bootstrap-modal

我在Laravel中遇到了Bootstrap-Modal的问题。

简单的Modal效果很好,但是当我使用“基于变化的模态内容”时, 我收到一个错误:

此剧本在我的主要方面:

$('#renameModal').on('show.bs.modal', function (event) {
      var button = $(event.relatedTarget) 
      var recipient = button.data('entity_input') 
      var entity = button.data('entity')
      var modal = $(this)
      modal.find('.modal-body input').val(recipient)
    })

产生此错误:

  

VerifyCsrfToken.php第67行中的TokenMismatchException

我的代码中有行:{!! csrf_field() !!},但是当我 使用JQuery - 脚本,它不起作用。

任何人都知道如何解决问题?

模态代码:

<div class="modal fade" id="renameModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">Umbenennen</h4>
                  </div>
                  <div class="modal-body">
                    <form action="{{ url('user_rename/'.$user[0]->id) }}" method="POST">
                       {!! csrf_field() !!}
                       <input type="text" class="form-control" id="recipient-name">
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Abbrechen>
                    <button type="submit" id="rename-user{{ $user[0]->id }}" class="btn btn-primary">Speichern</button>
                  </div>
                  </form>
                </div>
              </div>
            </div>

's格式错误; - (

1 个答案:

答案 0 :(得分:0)

这是因为您正在更改隐藏的csrf标记的值。

# all_on_start: false               # run all tests in group on startup, default: true
# all_after_pass: true              # run all tests in group after changed specs pass, default: false
# cli: '--test'                     # pass arbitrary Minitest CLI arguments, default: ''
# test_folders: ['tests']           # specify an array of paths that contain test files, default: %w[test spec]
# include: ['lib']                  # specify an array of include paths to the command that runs the tests
# test_file_patterns: %w[test_*.rb] # specify an array of patterns that test files must match in order to be run, default: %w[*_test.rb test_*.rb *_spec.rb]
# spring: true                      # enable spring support, default: false
# zeus: true                        # enable zeus support; default: false
# drb: true                         # enable DRb support, default: false
# bundler: false                    # don't use "bundle exec" to run the minitest command, default: true
# rubygems: true                    # require rubygems when running the minitest command (only if bundler is disabled), default: false
# env: {}                           # specify some environment variables to be set when the test command is invoked, default: {}
# all_env: {}                       # specify additional environment variables to be set when all tests are being run, default: false
# autorun: false                    # require 'minitest/autorun' automatically, default: true
options = {
  spring: 'bin/rails test', # NOTE: true = run all tests on every run
  all_on_start: false,
  all_after_pass: false
}
guard :minitest, options do
  # with Minitest::Unit
  watch(%r{^test/(.*)\/?test_(.*)\.rb$})
  watch(%r{^test/(.*)\/.+_test\.rb$})
  watch(%r{^lib/(.*/)?([^/]+)\.rb$})     { |m| "test/#{m[1]}test_#{m[2]}.rb" }
  watch(%r{^test/test_helper\.rb$})      { 'test' }
  watch(%r{^test/.+_test\.rb$})

  # watch('test/test_helper.rb')                        { 'test' }
  watch('config/routes.rb')                           { 'test/routing' }
  watch('app/controllers/application_controller.rb')  { 'test/controllers' }
  watch(%r{^app/(.+)\.rb$})                           { |m| "test/#{m[1]}_test.rb" }
  watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$})          { |m| "test/#{m[1]}#{m[2]}_test.rb" }
  watch(%r{^lib/(.+)\.rb$})                           { |m| "test/lib/#{m[1]}_test.rb" }
  watch(%r{^app/controllers/(.+)_(controller)\.rb$})  { |m| ["test/routing/#{m[1]}_routing_test.rb", "test/#{m[2]}s/#{m[1]}_#{m[2]}_test.rb", "test/system/#{m[1]}_test.rb"] }

  # with Minitest::Spec
  watch(%r{^spec/(.*)_spec\.rb$})
  watch(%r{^lib/(.+)\.rb$})         { |m| "spec/#{m[1]}_spec.rb" }
  watch(%r{^spec/spec_helper\.rb$}) { 'spec' }
end

<强> {!! csrf_field()!!} 会生成类似这样的内容并高于输入#recipient-name

modal.find('.modal-body input').val(recipient)

在您的Javascript更改<input type="hidden" name="_token" value="some-random-token"> modal.find('.modal-body input')。还值得注意的是,您没有给收件人姓名输入一个实际的字段名称。