首页 > 解决方案 > Ruby on Rails 上的数据加载停止

问题描述

我使用 Ruby on Rails 5 作为后端,使用 Angular 5 作为前端。我在 localhost 上运行应用程序进行测试。问题是铁轨不时挂起。正因为如此,Angular 停止显示信息。并且需要重新启动 Ruby on Rails 进程才能再次开始显示信息。但是要重新启动,您必须先终止该进程,然后再次运行。没有加载任何严重的内容,因此应用程序冻结。

正因为如此,才会出现这样的错误:

GET http://localhost:3000/users 504(网关超时)无法加载http://localhost:3000/users:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问源“ http://localhost:4200 ”。响应具有 HTTP 状态代码 504。

如何解决?

cors.rb:

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '127.0.0.1:4200', 'localhost:4200'
    resource '*',
             :headers => :any,
             :expose  => ['access-token', 'expiry', 'token-type', 'uid', 'client'],
             :methods => [:get, :post, :options, :delete, :put]
  end
end

数据库.yml:

default: &default
  adapter: oracle_enhanced
  database: ********************************************
  username: **********
  password: **********

development:
  <<: *default
  database: ********************************************
  username: **********
  password: **********


test:
  <<: *default

production:
  <<: *default

宝石文件:

source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.6'
# Use sqlite3 as the database for Active Record
gem 'ruby-oci8'
gem 'activerecord-oracle_enhanced-adapter'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
gem 'rack-cors'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platform: :mri
end

group :development do
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem 'omniauth'
gem 'devise_token_auth'
gem 'devise-i18n'
gem 'mini_magick'
gem 'logs'
gem 'logstasher'

group :development do
  gem 'capistrano', require: false
  gem 'capistrano-rvm', require: false
  gem 'capistrano-rails', require: false
  gem 'capistrano-bundler', require: false
  gem 'capistrano3-puma', require: false
end

如果您需要展示其他内容,请写

标签: ruby-on-railsruby

解决方案


你需要分析你的请求

具体检查请求源头值和响应头Access-Control-Allow-Origin

如果这两个值不同,您的请求将被阻止

要求

回复

说明在

https://github.com/cyu/rack-cors


推荐阅读