gpt4 book ai didi

来自 Rails Controller 的 Javascript 响应导致未捕获的语法错误 : Unexpected token :

转载 作者:行者123 更新时间:2023-11-30 20:00:08 26 4
gpt4 key购买 nike

我有一个创建关系的表单,提交表单时,应返回添加新连接节点的 javascript 代码以及将它们连接到 cytoscape 图的关系:

形式:

<%= form_for Relation.new, :url => url_for(:controller => 'relations', :action => 'add_dependency'), remote: true do |f| %>
<%= f.hidden_field :to_id, :value => @article.id %>
<%= f.hidden_field :graph, :value => 1 %>
<%= f.select :from_id, [], {}, {class: "select-article"} %>
<%= f.submit "Add a dependency of this article." %>
<% end %>

Controller 代码:

  def add_dependency
@relation = Relation.find_or_create_by(relation_params)
@relation.user << current_user

respond_to do |format|
if @relation.save
elements = json_for_cytoscape(@relation.from.self_and_all_dependencies_of_depth_and_less(3))
format.json { render :show, status: :created, location: @relation }
format.js { render js: "ancestors.add( #{elements} ); console.log('Hello');" }
else
format.json { render json: @relation.errors, status: :unprocessable_entity }
end
end
end

我在 javascript 控制台中收到此错误(并且没有“Hello”):

Uncaught SyntaxError: Unexpected token :
at processResponse (rails-ujs.self-8944eaf3f9a2615ce7c830a810ed630e296633063af8bb7441d5702fbe3ea597.js?body=1:244)
at rails-ujs.self-8944eaf3f9a2615ce7c830a810ed630e296633063af8bb7441d5702fbe3ea597.js?body=1:173
at XMLHttpRequest.xhr.onreadystatechange (rails-ujs.self-8944eaf3f9a2615ce7c830a810ed630e296633063af8bb7441d5702fbe3ea597.js?body=1:228)

这是响应:

ancestors.add( {:edges=>[], :nodes=>[{:data=>{:id=>200, :title=>"Test Yourself: Area & arc length using calculus", :href=>"http://localhost:3000/articles/200", :rank=>0.000459770114943, :color=>"grey"}}]} ); console.log('Hello');

最佳答案

我通过添加 to_json 解决了这个问题:

json_for_cytoscape(@relation.from.self_and_all_dependencies_of_depth_and_less(3)).to_json

关于来自 Rails Controller 的 Javascript 响应导致未捕获的语法错误 : Unexpected token :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53469521/

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