我完全停留在這一個和搜索小時沒有出現結果。當我嘗試執行ajax請求時,出現406錯誤。這裏是我的代碼..Rails返回406不可接受的阿賈克斯請求
層級控制器
def index
series = Series.find_by_id(params[:series_id])
respond_to do |format|
format.json { render :json => series.tiers }
end
end
的Javascript
$('#series_id').change ->
series_id = $('#series_id').val()
$.get '/admin/tiers', {series_id:series_id}, (result) ->
element = $('select[name *= "tier_id"]')
element.empty()
$.each result, (index, item) ->
element.append $('<option/>', value:item.id, text: item.title)
,這裏是我從Rails服務器
Started GET "/admin/tiers/1" for 127.0.0.1 at 2013-01-21 14:19:38 +1100
Processing by Admin::TiersController#index as JSON
Parameters: {"series_id"=>"1"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
(0.1ms) begin transaction
(0.5ms) UPDATE "users" SET "last_request_at" = '2013-01-21 14:19:38.827810', "perishable_token" = 'y0TMicsW2l4zgSiQcJx', "updated_at" = '2013-01-21 14:19:38.829452' WHERE "users"."id" = 1
(2.5ms) commit transaction
Tier Load (0.3ms) SELECT "tiers".* FROM "tiers" WHERE "tiers"."id" IS NULL LIMIT 1
Tier Load (0.3ms) SELECT "tiers".* FROM "tiers"
Series Load (0.2ms) SELECT "series".* FROM "series"
Series Load (0.2ms) SELECT "series".* FROM "series" WHERE "series"."id" = 1 LIMIT 1
Completed 406 Not Acceptable in 30ms (ActiveRecord: 4.4ms)
真的不得到錯誤知道什麼是填充它,控制器看起來很好,它看起來像內容類型是json。
我還使用軌道3.2.11
原因是因爲您不再對特定請求格式做出響應。你忽略了任何請求的格式,並且無論如何渲染JSON輸出。 通常,Rails中的格式是通過將格式附加到URL的擴展名來決定的。如果您要在路徑末尾使用.json調用相同的URL,它也可以工作。 – datashaman