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 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?
    • ¥100 求三轴之间相互配合画圆以及直线的算法
    • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
    • ¥15 名为“Product”的列已属于此 DataTable
    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 自己瞎改改,结果现在又运行不了了