Tôi nhận được một POST https://heroku_client_path.herokuapp.com/login 405 (Not Allowed)
cho một ứng dụng với một api Rails Ember.js (cả trên Heroku)Ember POST 405 (không được phép) w/Rails API
Khi xử lý một tên đăng nhập hoặc đăng ký. Tôi cảm thấy URL yêu cầu phải là đường dẫn/thông tin máy chủ heroku khi tôi đặt sốvar cấu hình heroku của mình và không phải là URL khách hàng heroku được hiển thị ở trên.
Tôi tin rằng tôi đã thiết lập CORS đúng cách.
Tôi đang sử dụng Ember-CLI. Tôi tay cán auth nó không phải là đơn giản-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;
};
adapter/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();
controllers/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 ray bên - 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
Điều này trông giống như vấn đề đường ray. Bạn đã thử nghiệm chương trình phụ trợ với người đưa thư hoặc curl chưa? Tuyến đường có hiện diện không? Bạn đang cố gắng yêu cầu html khi nó phải là json? – user2105103
@ user2105103 Tôi đã thử nghiệm với người đưa thư và nó xử lý yêu cầu POST tốt. Nó trả về phản hồi JSON đúng. Mặc dù URL máy chủ đường ray là URL máy chủ không được gửi. Thậm chí đã cố gắng mã hóa toàn bộ URL thay cho 'đăng nhập' cho hành động đó trong bộ điều khiển mà không có may mắn. – Jadam
Bạn có thể thử sử dụng tiện ích mở rộng này (https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi) cho chrome để thoát khỏi điều tra CORS không? –