Jad*_*dam 7 ruby-on-rails heroku cors ember.js ember-cli
我正在获得一个POST https://heroku_client_path.herokuapp.com/login 405 (Not Allowed)带有Rails api的Ember.js应用程序(两者都在heroku上)
处理登录或注册时.我觉得请求URL应该是heroku服务器路径/登录,因为我设置了我的ADAPTER_URLheroku配置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;
};
Run Code Online (Sandbox Code Playgroud)
适配器/ 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')
});
Run Code Online (Sandbox Code Playgroud)
brocfile.js
var EmberApp = require('ember-cli/lib/broccoli/ember-app');
var app = new EmberApp({
dotEnv: {
clientAllowedKeys: ['ADAPTER_URL']
}
});
module.exports = app.toTree();
Run Code Online (Sandbox Code Playgroud)
控制器/的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));
},
}
});
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
在这一行:
adapterURL: process.env.ADAPTER_URL
Run Code Online (Sandbox Code Playgroud)
在哪里process.env.ADAPTER_URL定义的?
看起来您走在正确的轨道上,重写host适配器ActiveModelAdapter以使用您的非客户端 URL。
| 归档时间: |
|
| 查看次数: |
652 次 |
| 最近记录: |