我需要在我的博客上按ID显示JSON文件中的评论。

huangapple go评论71阅读模式
英文:

I need to show json file comments by ID on my blog

问题

我不知道如何调用JSON文件,以便它会出现在博客中,我尝试了这种方法,但它不起作用。我认为我需要使用each循环,但我得到的唯一的东西是错误。

$.ajax({ 
    type: "GET", 
    url: "https://jsonplaceholder.typicode.com/comments", 
    dataType: 'json', 
    success: function (comment) {
        console.log(comment);
        commentsOn();
    }
});

function commentsOn(comments) {
    $("#comm").append(
        "<div class='card my-3'>postId:" + comments.postId +
        "<h6 class='card-header'>ID:" + comments.id + "</div>" +
        "<div class='card-body'>Name:" + comments.name + "</div>" +
        "<p class='email'>Email:" + comments.email + "</p>" +
        "<textarea class='form-control comment-text'>Body:" + comments.body + "</textarea>" + "</div>"
    );
}

希望这对你有帮助。

英文:

I dont know how to call the JSON file so it will appear in the blog, I tried this method but it wont work. I think I need to use the each loop, but the only thing I get is error.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$.ajax({ type: &quot;GET&quot;, url: &quot;https://jsonplaceholder.typicode.com/comments&quot;, dataType: &#39;json&#39;, success: function (comment) {
    console.log(comment);
    commentsOn();
  }
});
 
function commentsOn(comments) {
  $(&quot;#comm&quot;).append(
    &quot;&lt;div class=&#39;card my-3&#39;&gt;postId:&quot; + comments.postId +
    &quot;&lt;h6 class=&#39;caard-header&#39;&gt;ID:&quot; + comments.id + &quot;&lt;/div&gt;&quot; +
    &quot;&lt;div class=&#39;card-body&#39;&gt;Name:&quot; + comments.name + &quot;&lt;/div&gt;&quot; +
    &quot;&lt;p class=&#39;email&#39;&gt;Email:&quot; + comments.email + &quot;&lt;/p&gt;&quot; +
    &quot;&lt;textarea class=&#39;form-control comment-text&#39;&gt;Body:&quot; + comments.body + &quot;&lt;/textarea&gt;&quot; + &quot;&lt;/div&gt;&quot;

  );
}

<!-- end snippet -->

答案1

得分: 1

你可以使用$.getJSON()来获取数据,然后使用.forEach()将数据添加到DOM中。试试这个:

$.getJSON('https://jsonplaceholder.typicode.com/comments', function (data) {
   data.forEach(item => {
      $("#comm").append(
         `<div class="card my-3">
            postId: ${item.postId}
            <h6 class="card-header">ID: ${item.id}</h6>
            <div class="card-body">Name: ${item.name}</div>
            <p class="email">Email: ${item.email}</p>
            <textarea class="form-control comment-text">Body: ${item.body}</textarea>
         </div>`
      );
   });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="comm"></div>
英文:

You can use $.getJSON() to get the data and the use .forEach() to add the data to the DOM. Try this

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$.getJSON(&#39;https://jsonplaceholder.typicode.com/comments&#39;, function (data) {
   	data.forEach(item =&gt; {
      	$(&quot;#comm&quot;).append(
	        `&lt;div class=&quot;card my-3&quot;&gt;
		        postId: ${item.postId}
		        &lt;h6 class=&quot;caard-header&quot;&gt;ID: ${item.id}&lt;/div&gt;
		        &lt;div class=&quot;card-body&quot;&gt;Name: ${item.name}&lt;/div&gt;
		        &lt;p class=&quot;email&quot;&gt;Email: ${item.email}&lt;/p&gt;
		        &lt;textarea class=&quot;form-control comment-text&quot;&gt;Body: ${item.body}&lt;/textarea&gt;
		    &lt;/div&gt;`
      	);
    });
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;comm&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月3日 21:25:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定