你好,这是我在这里的第一篇文章!
我已经尝试调试这个问题几天了,但无法弄清楚。
当我向 Rails api 发出发布请求时,我收到了以前从未见过的错误:
Started POST "/owners" for ::1 at 2021-01-12 11:24:15 -0500
(1.0ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by OwnersController#create as */*
Parameters: {"email"=>"adam", "password"=>"[FILTERED]", "owner"=>{"email"=>"adam"}}
HTTP Origin header (http://localhost:3000) didn't match request.base_url (http://localhost:3001)
Completed 422 Unprocessable Entity in 0ms (ActiveRecord: 1.8ms | Allocations: 476)
ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
就像我说的,我以前从未见过这种情况,我不知道我是如何造成它的。我没有使用代理服务器,我在这个项目中尝试的唯一可能搞砸的新事情是我安装了 devise gem,但决定不使用它并删除它。
我尝试过的事情:
确保我没有待处理的迁移:
检查我的路线:
Rails.application.routes.draw do
resources :owners
resources :dogs
post 'login', to: 'sessions#create'
end
然后我认为这可能是一个cors问题:
require_relative 'boot'
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Backend
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 6.0
# Settings in config/environments/* take precedence over those specified here.
# Application configuration can go into files in config/initializers
# -- all .rb files in that directory are automatically loaded after loading
# the framework and any gems in your application.
config.api_only = true
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource(
'*',
headers: :any,
methods: [:get, :patch, :put, :delete, :post, :options, :head]
)
end
end
end
end
然后我尝试在谷歌上搜索有关无效真实性令牌和http原始标头的内容,但找不到有用的解决方案或我可以理解的解决方案。
(最后一点:我尝试将其从发布请求更改为获取请求,它有效,但发布会导致错误)
感谢您提前提供任何建议