2015-05-12 71 views
2

我有一個功能規格,作爲頁面加載的一部分。 我打了一個本地的URL來獲取更多的數據。Rails水豚功能規格否'Access-Control-Allow-Origin'

即使用http://fullcalendar.io/ 它加載它是通過AJAX

events: { 
      url: 'calendar_events.json', 
      type: 'GET', 
      error: function(response) { 
      ... 
      }}, 

我越來越

XMLHttpRequest cannot load http://localhost:3000/calendar_events.json?start=2014-01-20&end=2014-01-27. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 404. 

不完全知道如何解決事件。我試着加入

response.headers['Access-Control-Allow-Origin'] = '*' 
    response.headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS' 
    response.headers['Access-Control-Request-Method'] = '*' 
    response.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization' 

去我的應用程序控制器上的before_filter只是爲了看,但仍然得到同樣的問題。

注:我用的水豚,WebKit的

+0

http://stackoverflow.com/a/6120260/3274024你可以檢查出來,也許這是什麼錯了請求。 – jfornoff

+0

真的不想把它改成jsonp – dboyd68

+0

也許跨域觸發器就夠了。不知道我是否明白爲什麼你要訪問本地網址,是否可以不在服務器端立即渲染它? – jfornoff

回答

0

設置這些將是你的測試環境,因爲你不會希望在生產開闢起源的最好方法。

test.rb

config.action_dispatch.default_headers.merge!(
    { 
     'Access-Control-Allow-Origin' => '*', 
     'Access-Control-Allow-Methods' => 'POST, PUT, DELETE, GET, OPTIONS', 
     'Access-Control-Max-Age' => "1728000", 
    'Access-Control-Allow-Headers' =>'Origin, X-Requested-With, Content-Type, Accept, Authorization' 
    }) 

至於你的問題,我懷疑你是「save_and_open_page」在這種情況下,僅僅是根據你的水豚asset_path渲染錯誤,而不是實際的問題後收到此錯誤與你的測試案例。

+0

你知道爲Rails 3設置默認標題的方式嗎? – nesiseka

+0

對於單個請求,它沿着「get desired_pa​​th,params_hash,{'Access-Control-Allow-Origin'=>'*'}。我懷疑設置默認值不適用於您? –

+0

是的,default_headers僅適用於Rails 4。 – nesiseka