1

Simple: on one side Angularjs running on a server

'use strict'

### Sevices ###

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

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

on the other side a rails back end running on another server

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'

I don t get it because I already have a backbonejs running properly with this backend.. backbonejs, doesn t send an OPTIONS request though...

in my rails project I set up my Header directly in the code for now because I m using Webrick for development:

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

Found the solution by myself:

    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)
])
Mikael
  • 2,155
  • 1
  • 18
  • 42
  • 3
    Please answer your own question in the "answer" section below this comment, not inline in the question... – awendt Apr 12 '13 at 08:15

2 Answers2

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)
])
Erik J
  • 822
  • 8
  • 22
Mikael
  • 2,155
  • 1
  • 18
  • 42
  • 2
    Sure, delete $httpProvider.defaults.headers.common["X-Requested-With"] makes AngularJS stop sending the option request. – Mikael Sep 09 '13 at 07:58
  • It was valid for the version released before Apr 12 '13 That may be the reason why it does not work anymore. – Mikael May 29 '14 at 02:32
0

This helped me found here: 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 = {};
}]);
Community
  • 1
  • 1