我收到了 POST https://heroku_client_path.herokuapp.com/login 405 (Not Allowed)
对于带有 Rails api 的 Ember.js 应用程序(均在 heroku 上)
处理登录或注册时。我觉得请求 URL 应该是 Heroku 服务器路径/login,因为我设置了 ADAPTER_URL
heroku 配置变量,而不是上面显示的 heroku 客户端 URL。
我相信我的 CORS 设置正确。
我正在使用 Ember-CLI。我手动滚动了身份验证,它不是简单例份验证。
环境.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();
Controller /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 侧 - 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
最佳答案
在这一行:
adapterURL: process.env.ADAPTER_URL
在哪里
process.env.ADAPTER_URL
定义?似乎您在正确的 rails 上覆盖
host
在 ActiveModelAdapter
适配器以使用您的非客户端 URL。
关于ruby-on-rails - 带有 Rails API 的 Ember POST 405(不允许),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30315771/