Rails,jQuery,Ajax - 无法发送自定义标头

时间:2015-11-25 16:47:21

标签: jquery ruby-on-rails json ajax request

我正在尝试将自定义标头发送到rails api,但我一直在获取这些愚蠢的选项!

  

ActionController :: RoutingError(没有路由匹配[OPTIONS]“/ api / v1 / surveys”)

我已经启用了要在API application_controller.rb中发送的标头,至少我认为我做了,其中包含以下代码:

before_filter :allow_cross_domain

private
def allow_cross_domain
  headers['Access-Control-Allow-Origin'] = '*'
  headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'
  headers['Access-Control-Allow-Headers'] = 'X-User-Token, X-User-Email, Origin, Content-Type, Accept, Authorization, Token'
end

我正在尝试发送X-User-TokenX-User-Email标题。

所以在我的客户端,我拨打以下电话:

$.ajax({
  headers: {
    'Accept': 'application/json',
    'Content-type': 'application/x-www-form-urlencoded',
    'X-User-Email': 'my@email.com',
    'X-User-Token': 'fj4402SDgsGDS42'
  },
  url: Host.address + '/api/v1/surveys',
  type: 'GET',
}).done(function(response){});

我也试过beforeSend

$.ajax({
  url: Host.address + '/api/v1/surveys',
  type: 'GET',
  beforeSend: function(xhr) {
    xhr.setRequestHeader('Accept','application/json');
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xhr.setRequestHeader('X-User-Email', 'my@email.com');
    xhr.setRequestHeader('X-User-Token', 'fj4402SDgsGDS42');
  },
}).done(function(response){}

只有在我发送X-User-EmailX-User-Token时才会中断。当我仅发送AcceptContent-type标头时,它可以正常工作。

有人知道可能导致此问题的原因吗?

编辑:
我没有提到我正在使用Devise进行用户身份验证 添加survey的路由(我猜你不需要全部:)

Rails.application.routes.draw do
  devise_for :users, skip: [:registration, :sessions]

  namespace :api, as: false do
    namespace :v1, as: false do
      resources :responses, only: [:index, :new, :create]  
      resources :surveys, only: [:index, :create, :show]
    end
  end
end
    surveys GET    /api/v1/surveys(.:format)                api/v1/surveys#index
            POST   /api/v1/surveys(.:format)                api/v1/surveys#create
     survey GET    /api/v1/surveys/:id(.:format)            api/v1/surveys#show

我在浏览器控制台中遇到的错误是:

  

选项http://localhost:3003/api/v1/surveys

     

XMLHttpRequest无法加载http://localhost:3003/api/v1/surveys。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许原点“http://localhost:3000”访问。响应的HTTP状态代码为404.

2 个答案:

答案 0 :(得分:0)

尝试在application.rb文件中设置自定义标头。

config.action_dispatch.default_headers = {
    'Access-Control-Allow-Origin' => 'YOUR_URL',
    'Access-Control-Request-Method' => %w{GET POST OPTIONS}.join(",")
  } 

在app / controllers / api / v1 / application_controller.rb文件中设置标题如下

before_filter :allow_cross_domain

def allow_cross_domain
    headers['Access-Control-Allow-Origin'] = '*' 
    headers['Access-Control-Expose-Headers'] = 'ETag'
    headers['Access-Control-Allow-Methods'] = 'GET, POST, PATCH, PUT, DELETE, OPTIONS, HEAD'
    headers['Access-Control-Allow-Headers'] = '*,x-requested-with,Content-Type,If-Modified-Since,If-None-Match'
    headers['Access-Control-Max-Age'] = '86400'
end 

最后在你的app / controllers / api / v1 / controller_your_are_accessing_from_browser.rb里面打电话:

before_filter :allow_cross_domain 

在导轨指南中查看3.8 Configuring Action Dispatch。还要检查浏览器中的控制台日志,以查看从上述请求中获得的错误。

答案 1 :(得分:0)

您可以尝试在路线中添加:options

namespace :api, as: false do
  namespace :v1, as: false do
    resources :responses, only: [:index, :new, :create] 
    match 'surveys' => "surveys#index", via: [:get, :options] 
    resources :surveys, only: [:index, :create, :show]
  end
end