Rails,请求的资源上不存在“Access-Control-Allow-Origin"标头 [英] Rails, No 'Access-Control-Allow-Origin' header is present on the requested resource

查看:62
本文介绍了Rails,请求的资源上不存在“Access-Control-Allow-Origin"标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Angular 上有应用程序,它使用服务器 Ruby on Rails/oAuth 后端.在控制台中注册用户时,我收到这些错误.

I have apps on Angular, which uses the server Ruby on Rails/oAuth backend. When registering a user in the console, I get these errors.

POST*.herokuapp.com/api/users 500 (Internal Server Error)  *.herokuapp.com/api/users:1 
Failed to load *.herokuapp.com/api/users: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '*.herokuapp.com' is therefore not allowed access. The response had HTTP status code 500.   /registration:1 

{
 "isTrusted": true     /auth.ts:95
}

以及其他发生的错误

{"error":"invalid_grant","error_description":"The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client."}
Error: Uncaught (in promise): {"error":"invalid_grant","error_description":"The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client."}

这是我的应用程序.rb

Here is my application.rb

 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 Dkeeper
   class Application < Rails::Application
     config.middleware.use Rack::Cors do
       allow do
         origins '*'
         resource '*', headers: :any, methods: [:get, :post, :put, :patch, :delete, :options, :head]
       end
     end
   end
 end

这里是 user_controller.rb

Here user_controller.rb

class Api::UsersController < ApiController
before_action :find_user, only: [:show, :update, :destroy]

def index
  @users = User.all

if @users.count(:all) > 0
  render
else
  render json: { message: 'No Users Found' }, status: 200
end
end

def show
 render
end

def create
@user = User.new(user_params)

if @user.save
  origin = request.headers['origin']
  ApplicationMailer.registration_confirmation(@user, origin).deliver
  render :show, status: :ok
else
  render json: {
    message: 'Registration failed',
    errors: @user.errors.full_messages
  }, status: 422
end
end

def update
if @user.update(user_params)
  render :show
else
  render json: {
    message: 'Could not update profile',
    errors: @user.errors.full_messages
  }, status: 422
end
end

def destroy
if @user.destroy
  render
else
  render json: {
    message: 'Could not delete user',
    errors: @user.errors.full_messages
  }, status: 422
end
end

def confirm_email
user = User.find_by_confirm_token(params[:id])
if user
  user.update_attribute(:email_confirmed, true)
  render json: {
    message: 'Email address has already confirmed',
    errors: user.errors.full_messages
  }, status: 200
else
  render json: {
    message: 'Email confirmation failed'
  }, status: 422
end
end

private

def user_params
  params.require(:user).permit(:email, :first_name, :last_name, :password, :password_confirmation)
end

def find_user
  @user = User.find(params[:id])
end

end

当我登录时,用户被添加到数据库中,但我在控制台中收到一个错误.

When I log in, the user is added to the database, but I get an error in the console.

感谢您的帮助.

推荐答案

这是 CORS 错误.您需要在服务器端启用 CORS 并在客户端传递正确的标头.也谷歌同源政策"

This is a CORS error. You need to enable CORS on the server side and past the correct headers on the client side. Also google "Same Origin Policy"

这篇关于Rails,请求的资源上不存在“Access-Control-Allow-Origin"标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆