带有Rails API的Ember POST 405(不允许)

时间:2015-05-19 02:29:44

标签: ruby-on-rails heroku ember.js cors ember-cli

我得到一个带有Rails api的Ember.js应用程序的POST https://heroku_client_path.herokuapp.com/login 405 (Not Allowed)(两者都在heroku上)

处理登录或注册时。我觉得请求URL应该是heroku服务器路径/登录,因为我设置了ADAPTER_URL heroku config var,而不是上面显示的heroku客户端URL。

我相信我已正确设置CORS。

我正在使用Ember-CLI。我用手滚动认证它不是Simple-Auth。

environment.js:

module.exports = function(environment) {
  var ENV = {
    modulePrefix: 'client-rantly',
    environment: environment,
    baseURL: '/',
    adapterURL: process.env.ADAPTER_URL,
    locationType: 'auto',
    EmberENV: {
      FEATURES: {
      }
    },
  }; 

  if (environment === 'development') {
  }

  if (environment === 'production') {   
  }

  return ENV;
};

适配器/ application.js中:

import DS from 'ember-data';
import ENV from '../config/environment';

export default DS.ActiveModelAdapter.extend({
  host: ENV.adapterURL || ENV.ADAPTER_URL,
  headers: function () {
    return {
      'auth_token': localStorage.getItem('authToken')
    };
  }.property('authToken')
});

brocfile.js

var EmberApp = require('ember-cli/lib/broccoli/ember-app');

var app = new EmberApp({
  dotEnv: {
    clientAllowedKeys: ['ADAPTER_URL']
  }
});
module.exports = app.toTree();

控制器/ application.js中

import Ember from 'ember';

export default Ember.Controller.extend({
  needs: ['search'],
  isAuthenticated: false,

  init: function() {
    var authToken = localStorage.getItem('authToken');
    if(authToken) {
      this.isAuthenticated = true;
    }
  },

  actions: {

    login: function () {
      var credentials = {
        email: this.get('email'),
        password: this.get('password')
      };

      this.set('errorMessage', null);
      return Ember.$.post('login', credentials).then(function(response){
        this.set('errorMessage', response.error);
        if (response.auth_token) {
          localStorage.setItem('authToken', response.auth_token);
          localStorage.setItem('userId', response.user_id);
          this.set('isAuthenticated', true);
          location.reload();
        }
      }.bind(this));
    },
  }
});

cors rails side - config / application.rb

require File.expand_path('../boot', __FILE__)
require 'rails/all'

Bundler.require(*Rails.groups)

module ServerRantly
  class Application < Rails::Application
    config.active_record.raise_in_transactional_callbacks = true
    config.autoload_paths << Rails.root.join('lib')

    config.middleware.insert_before 0, "Rack::Cors", :debug => true, :logger => (-> { Rails.logger }) do
      allow do
        origins '*'

        resource '/cors',
          :headers => :any,
          :methods => [:post],
          :credentials => true,
          :max_age => 0

        resource '*',
          :headers => :any,
          :methods => [:get, :post, :delete, :put, :options, :head],
          :max_age => 0
      end
    end
  end
end

1 个答案:

答案 0 :(得分:1)

在这一行:

adapterURL: process.env.ADAPTER_URL

process.env.ADAPTER_URL定义在哪里?

您似乎正在使用host适配器覆盖ActiveModelAdapter以使用非客户端网址。