gpt4 book ai didi

javascript - 对 Rails 应用程序进行 d3.json 调用时收到 406 错误

转载 作者:行者123 更新时间:2023-11-28 04:15:18 25 4
gpt4 key购买 nike

我有一个网页尝试使用 d3 向 Rails 应用程序请求 json 数据。由于某种原因,我不断收到 406 错误(“ Not Acceptable ”),并且在 Controller 内,request.content_typenilrequest.format 给我:

#<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

Controller :

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

最佳答案

该行有问题

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

你必须

将扩展名“json”添加到网址

或更改路由文件

资源:源,默认:{格式::json}

关于javascript - 对 Rails 应用程序进行 d3.json 调用时收到 406 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45921903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com