2011-12-09 38 views
6

當我們回到使用render :json =>@profiles,該 輸出將返回一個錯誤406所需的結果JSON輸出。 如何避免'406 Not Acceptable'錯誤?ROR返回JSON與406不接受錯誤

+1

你能否提供您所使用的控制器代碼? –

+0

一張圖片勝過1000字,但有些代碼會讓您的問題得到解答! –

回答

11

我肯定不只這你有this problem

說明:

說你的控制器僅返回JSON回答

def action 
    # call 
    respond_to do |format| 
    format.json { render json: results } 
    end 
end 

這會盡快返回JSON:

  • /path_to_action.json
  • /path_to_action被稱爲與頭Content-Type:application/json;和其他可能的頭文件類型(例如X-Requested-With:XMLHttpRequest

否則,它會返回406 Not Acceptable錯誤。

爲了避免這個問題,如果你的控制器只返回JSON,寫:

def action 
    # call 
    render json: results 
end 

否則,使用/path_to_action.json來代替。

0

這發生在我身上時,我的控制器動作有before_action :authenticate_user!,但是從未經認證的頁面調用此。

頁面本身試圖發出重定向。

驗證用戶或取出before_action解決了這個問題對我來說。