NewsAPIを使いJsonデータを取得するとところまでは以前の質問で出来たのですがJqueryでjsonデータをhtmlに表示するところでもハマってしまいました。errorは出てませんが表示がされません。
何回も質問すみません。毎回同じですが、なにかわかる方は回答お願いします。
log
1Started GET "/news/data" for ::1 at 2020-08-16 14:50:53 +0900 2Processing by NewsController#data as */* 3 Rendering news/data.js.erb 4 Rendered news/data.js.erb (Duration: 0.2ms | Allocations: 4) 5Completed 200 OK in 860ms (Views: 10.4ms | ActiveRecord: 0.0ms | Allocations: 1951)
controller
1 2require 'uri' 3require 'net/http' 4require 'json' 5 6def data 7 @END_POINT = 'https://newsapi.org/v2'.freeze 8 @REQUEST_URL = "#{@END_POINT}/top-headlines?sources=bbc-news&apiKey=自分のAPI".freeze 9 json = 10 URI.parse(@REQUEST_URL) 11 .then { |uri| Net::HTTP.get(uri) } 12 .then { |response| JSON.parse!(response) } 13 @data = json['articles'].html_safe 14end
datajs
1var data = <%= @data %>; 2var result = '<ul class="info">' 3data.forEach(function(e){ 4 result += '<li>' + e.title + '</li><hr>'; 5}); 6result += '</ul>' 7$('#result').html(result); 8
index
1<div id="result">no information...</div> 2<script> 3var f = function(){ 4 $.ajax({ 5 type: 'GET', 6 url: '/news/data' 7 }); 8}; 9setTimeout(f, 600000); 10f(); 11</script>
追記:デロッパーツールでこのような表示がありました
VM340:2 Uncaught SyntaxError: Unexpected token 'var' at DOMEval (DOMEval.js:38) at Function.globalEval (core.js:197) ... var data = var result += '<ul class="info">' ←ここを指してた data.forEach(function(e){
回答1件
あなたの回答
tips
プレビュー