1

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

'use strict'

### Sevices ###

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

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

在另一侧,在另一台服务器上运行的 rails 后端

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'

我不明白,因为我已经有一个backendjs 可以正常运行..backendjs,虽然没有发送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)
])
4

2 回答 2

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)
])
于 2013-04-15T04:54:23.747 回答
0

这帮助我在这里找到:AngularJS 对跨域资源执行 OPTIONS HTTP 请求

  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 = {};
}]);
于 2014-09-11T14:03:22.557 回答