RoutingError(没有路由匹配[OPTIONS]

时间:2013-04-12 06:29:29

标签: javascript ruby-on-rails ajax cross-domain options

简单: 一方面Angularjs在服务器上运行

'use strict'

### Sevices ###

angular.module('app.services', [])

.factory 'Contents', ($resource) ->
      Contents = $resource('http://127.0.0.1\\:3000/documents.json')

另一侧是在另一台服务器上运行的导轨后端

Started OPTIONS "/documents.json" for 127.0.0.1 at 2013-04-12 15:22:27 +0900

ActionController::RoutingError (No route matches [OPTIONS] "/documents.json"):
  actionpack (3.2.12) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.12) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.12) lib/rails/rack/logger.rb:32:in `call_app'
  railties (3.2.12) lib/rails/rack/logger.rb:16:in `block in call'
  activesupport (3.2.12) lib/active_support/tagged_logging.rb:22:in `tagged'
  railties (3.2.12) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.12) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.5) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.12) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.5) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.12) lib/action_dispatch/middleware/static.rb:62:in `call'
  railties (3.2.12) lib/rails/engine.rb:479:in `call'
  railties (3.2.12) lib/rails/application.rb:223:in `call'
  rack (1.4.5) lib/rack/content_length.rb:14:in `call'
  railties (3.2.12) lib/rails/rack/log_tailer.rb:17:in `call'
  rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
  /Users/mikael/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /Users/mikael/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /Users/mikael/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

我不明白,因为我已经有了一个使用这个后端正常运行的backbonej .. backbonejs,虽然不发送OPTIONS请求...

在我的rails项目中,我现在直接在代码中设置了Header,因为我正在使用Webrick进行开发:

class ApplicationController < ActionController::Base
  protect_from_forgery
  after_filter :set_access_control_headers

  def set_access_control_headers
    headers['Access-Control-Allow-Origin'] = '*'
    headers['Access-Control-Request-Method'] = '*'
    headers['Access-Control-Allow-Headers'] = '*'
  end
end

自己找到解决方案:

    App.config([
  '$routeProvider'
  '$httpProvider'
  '$locationProvider'

($routeProvider, $httpProvider, $locationProvider, config) ->

  $routeProvider

    .when('/contents', {templateUrl: '/partials/contents.html'})
    .when('/view1', {templateUrl: '/partials/partial1.html'})
    .when('/view2', {templateUrl: '/partials/partial2.html'})

    # Catch all
    .otherwise({redirectTo: '/contents'})

  delete $httpProvider.defaults.headers.common["X-Requested-With"]

  # Without server side support html5 must be disabled.
  $locationProvider.html5Mode(false)
])

2 个答案:

答案 0 :(得分:1)

App.config([
  '$routeProvider'
  '$httpProvider'
  '$locationProvider'

  ($routeProvider, $httpProvider, $locationProvider, config) ->

  $routeProvider

    .when('/contents', {templateUrl: '/partials/contents.html'})
    .when('/view1', {templateUrl: '/partials/partial1.html'})
    .when('/view2', {templateUrl: '/partials/partial2.html'})

    # Catch all
    .otherwise({redirectTo: '/contents'})

  delete $httpProvider.defaults.headers.common["X-Requested-With"]

  # Without server side support html5 must be disabled.
  $locationProvider.html5Mode(false)
])

答案 1 :(得分:0)

这有助于我在这里找到:AngularJS performs an OPTIONS HTTP request for a cross-origin resource

  app.config(['$httpProvider', function ($httpProvider) {
  //Reset headers to avoid OPTIONS request (aka preflight)
  $httpProvider.defaults.headers.common = {};
  $httpProvider.defaults.headers.post = {};
  $httpProvider.defaults.headers.put = {};
  $httpProvider.defaults.headers.patch = {};
}]);