dongzhao1930 2018-09-21 20:18
浏览 37

Javascript API仅在foreach循环中运行一次 - Laravel

My objective is to collect data (hash) for an API from my database, once collected, my js file uses that data for the API call. The API result is then shown on the blade.php page. At this moment in time, my API is only working for one result.

File: review.blade.php

@foreach($reviews as $review)
   <div class="container card-header">
      <h3>{{ $review->company }}</h3>
      <h5><a href="/reviews/{{ $review->id }}">{{ $review->title }}</a></h5>           
      <h5 id="source">{{ $review->hash }}</h5>                     
   </div>
   <div class="container card-body">
      <p id="content"></p>
   </div>
@endforeach

All of the review details from the DB are shown, without any problems.

JS Code:

function display () {

      // Assigning variables
      var hash = document.getElementById('source').innerHTML;

      // IPFS API
      ipfs.cat(hash, {buffer: true}, function (err, res) {
        if (err || !res) {
          return console.error('ipfs cat error', err, res)
        }

        document.getElementById('content').innerText = res.toString()
      })
    }  // function closes

    document.addEventListener('DOMContentLoaded', display())

The JS only runs once for a single hash. I'm not sure what to do. Any advice is welcome.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
    • ¥15 gdf格式的脑电数据如何处理matlab
    • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
    • ¥100 监控抖音用户作品更新可以微信公众号提醒
    • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
    • ¥70 2048小游戏毕设项目
    • ¥20 mysql架构,按照姓名分表
    • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
    • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
    • ¥15 linux驱动,linux应用,多线程