【问题标题】:Ember POST 405 (Not Allowed) w/ Rails API带有 Rails API 的 Ember POST 405(不允许)
【发布时间】:2015-07-30 16:47:20
【问题描述】:

我正在为带有 Rails api 的 Ember.js 应用程序获得 POST https://heroku_client_path.herokuapp.com/login 405 (Not Allowed)(都在 heroku 上)

在处理登录或注册时。我觉得请求 URL 应该是 heroku 服务器路径 /login,因为我设置了我的 ADAPTER_URL heroku config var,而不是上面显示的 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();

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 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

【问题讨论】:

  • 这看起来像是一个 Rails 问题。你用 postman 或 curl 测试过后端吗?路线存在吗?您是否尝试在它应该是 json 时请求 html?
  • @user2105103 我已经用邮递员进行了测试,它可以很好地处理 POST 请求。它返回正确的 JSON 响应。尽管使用的是未发送的 rails 服务器 URL。甚至尝试在控制器中为该操作硬编码完整的 URL 来代替“登录”。
  • 您可以尝试使用 this 扩展 chrome 以逃避 CORS 调查吗?

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


【解决方案1】:

在这一行:

adapterURL: process.env.ADAPTER_URL

process.env.ADAPTER_URL 在哪里定义?

您似乎走在了正确的轨道上,在 ActiveModelAdapter 适配器上覆盖 host 以使用您的非客户端 URL。

【讨论】:

  • 本地它指向本地主机,虽然在 Heroku 上我让它指向我的 Rails 服务器也托管在 Heroku 上,这就是为什么我觉得奇怪的是,即使它指向它仍然运行 POST Ember 客户端 URL。
猜你喜欢
  • 2015-02-10
  • 2023-04-05
  • 2014-05-23
  • 2014-06-22
  • 2016-07-25
  • 2021-09-30
  • 2011-04-03
  • 2018-12-20
  • 2015-11-16
相关资源
最近更新 更多