2013-05-21 93 views
0

我試圖讓我的瀏覽器的JSON響應,但我發現:爲什麼我的這個請求得到「406不可接受」?

完成406在4ms的不接受(ActiveRecord的:1.0ms的)

它應該工作。我在我的控制器下面的代碼:

respond_to :json 
    def get_current_value 
    stocks = Stockexchange.find(:all) 

    respond_with(stocks) do |format| 
    format.json { render :json => stocks.to_json} 
    end 
return 
end 
+1

您是否在提出請求時將'Accept'頭設置爲'application/json'?您是否使用通用HTTP客戶端對其進行了測試,以驗證它是服務器端問題? – toniedzwiedz

回答

4

強行進入Rails的使用JSON格式的迴應是添加.json於你請求的URL的最好方法。一旦你得到這個工作,你可以嘗試使用標題等。

0

這應該工作:

def get_current_value 
    @stocks = Stockexchange.find(:all) 
    respond_to do |format| 
    format.html # Presumes existence of get_current_value.html.erb 
    format.json { render :json => @stocks } 
    end 
end 

擺脫format.html的,如果你永遠只能希望它返回JSON。

相關問題