rails - 解决 rails 4 的 接口跨域问题

访问量: 50

让接口的response的header中返回一些东西就可以:

class Interface::ApplicationController < ActionController::Base

  after_filter :set_access_control_headers

  def set_access_control_headers
    headers['Access-Control-Allow-Origin']='*'
    headers['Access-Control-Allow-Methods']='POST, PUT, DELETE, GET, OPTIONS'
    headers['Access-Control-Request-Method']='*'
    headers['Access-Control-Allow-Headers']='Origin, X-Requested-With, Content-Type, Accept, Authorization'
  end

然后所有的接口都继承该接口。

或者使用gem:  

gem 'rack-cors'

订阅/RSS Feed

Subscribe

分类/category