简体   繁体   English

为什么我的加密 API 返回错误的字符串?

[英]Why does my crypto API return the wrong string?

I was wondering why the javascript code below does not return the correct information l need.我想知道为什么下面的 javascript 代码没有返回我需要的正确信息。 For example, l want a news title from the API but it gives me different information.例如,我想要来自 API 的新闻标题,但它给了我不同的信息。 To summarise, l want to target a specific title like [0] but it won't change.总而言之,我想定位一个特定的标题,如 [0],但它不会改变。

Second question is, is it possible to connect 2 different APIs that target different HTML elements in one code or would you have to do separate javascript functions?第二个问题是,是否可以在一个代码中连接针对不同 HTML 元素的 2 个不同 API,或者您是否必须执行单独的 javascript 函数?

I'm learning and any explanation would be helpful.我正在学习,任何解释都会有所帮助。

<div class="box" id="insert-news">
        <div class="title">Marketplace </div>
        <h2>Live News</h2>
        <p><span class='highlight'>News Article</span></p>
        <p>Information here</p>
    </div>
function getData(){
    fetch('https://api.coinstats.app/public/v1/news?skip=0&limit=10').then(response => {
        return response.json();
    }).then(data => {
        console.log(data.news[2].title);
        let newsTitle ='';
        data.news.map((values)=>{
            newsTitle = `<div class="title">Marketplace </div>
        <h2>Live News</h2>
        <p><span class='highlight'>News Article</span></p>
        <p>${values.title}</p>
    </div>`;
        })
        document.getElementById('insert-news').innerHTML = newsTitle;
    })
}

getData();

I think what you are trying to reach is to display all news inside insert-news container我认为您想要达到的是在insert-news容器中显示所有新闻

function getData() {
  fetch("https://api.coinstats.app/public/v1/news?skip=0&limit=10")
    .then((response) => {
      return response.json();
    })
    .then((data) => {
      document.getElementById("insert-news").innerHTML = data.news
        .map(
          (values) =>
            `<div class="title">Marketplace </div>
      <h2>Live News</h2>
      <p><span class='highlight'>News Article</span></p>
      <p>${values.title}</p>
  </div>`
        )
        .join("");
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM