2017-08-28 43 views
1

我有一個網頁試圖使用d3向json數據的rails應用程序發出請求。出於某種原因,我不斷收到一個錯誤406(「不接受」),並在控制器內,request.content_typenilrequest.format給我:接收406錯誤使d3.json調用rails應用程序

#<Mime::Type:0x00000002595c20 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html", @hash=4164709875014779265> 

我在做什麼錯?

中的JavaScript

url = '/sources/data/' + $('#source_id').val() + '/' + $(this).val(); 
    return d3.json(url, function(d) { 
    var color, format, height, pack, svg, width; 
    console.log(d); 
    //...do some stuff 

控制器:

class SourcesController < ApplicationController 
    before_action :set_source 

    def get_data 
    @count_data = Hash.new 
    case params[:type].downcase 
    when 'stems' 
    @count_data = JSON.parse @source.stem_cache 
    when 'words' 
    @count_data = JSON.parse @source.word_cache 
    when 'synonyms' 
    @count_data = JSON.parse @source.synonym_cache 
    end 
    puts "hello" 
    respond_to do |format| 
    format.json {render json: @count_data} 
    end 
end 
+0

請加樣品網址 –

+0

我認爲你可以得到答案這裏https://stackoverflow.com/questions/12320259/406-error-when-respond-with-json –

+0

謝謝,@artgb!我認爲指定content_type足夠了,但事實證明我需要在網址中添加'.json' - 並且您重新閱讀該文章的方向讓我嘗試瞭解這個事實。如果您將此作爲答案發布,我會很感激,所以我可以將其標記爲這樣。 –

回答

0

有在這一行的問題

url = '/sources/data/' + $('#source_id').val() + '/' + $(this).val(); 

你必須

添加擴展名 '的json' 到url

或更改路由文件

資源:源,默認:{格式:JSON}