Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Rails CORS:ActionController::RoutingError(没有与[选项]“批次”匹配的路由):_Javascript_Jquery_Ruby On Rails_Ruby - Fatal编程技术网

Javascript Rails CORS:ActionController::RoutingError(没有与[选项]“批次”匹配的路由):

Javascript Rails CORS:ActionController::RoutingError(没有与[选项]“批次”匹配的路由):,javascript,jquery,ruby-on-rails,ruby,Javascript,Jquery,Ruby On Rails,Ruby,我正在尝试在rails中执行跨平台请求 我的jquery代码如下:- <script> $.ajaxSetup({ headers: { 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') } }); $(document).ready(function () { $('#submit-button').click(function()

我正在尝试在rails中执行跨平台请求

我的jquery代码如下:-

<script>
    $.ajaxSetup({
      headers: {
        'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
      }
    });
    $(document).ready(function () {
        $('#submit-button').click(function() {
            $.ajax({
                type: "POST",
                url: "http://localhost:3000/batches",
                beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))},
                xhrFields: {
                    withCredentials: true
                },
                data: {     
                    batch: {
                      name: $("#name").val(),
                      course_id: $("#course_id").val(),
                      start_date: $("#start_date").val(),
                      end_date: $("#end_date").val(),
                      status: $("#batch_status").val(),
                      }
                },
                dataType: "JSON",
              error: function(error) {
                    console.log(error);
                      },
              success: function(data) {
                         console.log(data);
                         return false;
                      },
            })
        });
    })
</script>
但每次尝试使用表单添加记录时,rails日志中都会显示以下错误:-

提交第一个数据时出错

Started OPTIONS "/batches" for 127.0.0.1 at 2017-01-11 22:02:49 +0545
  ActiveRecord::SchemaMigration Load (0.5ms)  SELECT "schema_migrations".* FROM "schema_migrations"

ActionController::RoutingError (No route matches [OPTIONS] "/batches"):
一次以上提交数据时出错:-

Started OPTIONS "/batches" for 127.0.0.1 at 2017-01-11 22:08:50 +0545

ActionController::RoutingError (No route matches [OPTIONS] "/batches"):

有谁能帮我解决这个问题。

您需要配置Rails以支持CORS

一个解决方案是使用gem

需要将以下截图添加到config/application.rb

moduleyourapp
类应用程序:any,:methods=>[:get,:post,:options]
结束
结束
#轨道5
config.middleware.insert_在0之前,Rack::Cors do
允许做
起源“*”
资源“*”,:headers=>:any,:methods=>[:get,:post,:options]
结束
结束
结束
结束

是的,它起作用了,但它带来了新的问题。。。现在它说的是ActionController::InvalidAuthenticityToken(ActionController::InvalidAuthenticityToken):您需要确保正确传递了真实性令牌。检查此项:无法验证CSRF令牌的真实性。在16ms(ActiveRecord:0.0ms)内完成422个不可处理实体ActionController::InvalidAuthenticationToken(ActionController::InvalidAuthenticationToken):这是我使用跨平台请求提交表单时提供的完整错误…是否要查看我的表单?
Started OPTIONS "/batches" for 127.0.0.1 at 2017-01-11 22:08:50 +0545

ActionController::RoutingError (No route matches [OPTIONS] "/batches"):
module YourApp
  class Application < Rails::Application

    # ...

    # Rails 3/4

    config.middleware.insert_before 0, "Rack::Cors" do
      allow do
        origins '*'
        resource '*', :headers => :any, :methods => [:get, :post, :options]
      end
    end

    # Rails 5

    config.middleware.insert_before 0, Rack::Cors do
      allow do
        origins '*'
        resource '*', :headers => :any, :methods => [:get, :post, :options]
      end
    end

  end
end