简体   繁体   English

My Javascript JSON code in HTML page does not render the RESTful API array objects, although I get the data from the API. 我应该怎么办?

[英]My Javascript JSON code in HTML page does not render the RESTful API array objects, although I get the data from the API. What should I do?

enter image description here The problem is that the objects of the API are not being rendered in HTML, what did I do wrong?在此处输入图像描述问题是 API 的对象没有在 HTML 中呈现,我做错了什么?

       <button onclick = "showCountries()">Show Countries</button>
        <div id = "feed"></div>
        <script>
            function showCountries(){
                let xhr = new XMLHttpRequest()
                    xhr.open('GET', 'https://restcountries.com/v3.1/all', true)
                    xhr.onload = function(){
                    if(xhr.status == 200){
                    console.log('success')
                    let countries = JSON.parse(this.response)
                    countries.forEach(country=>{
                        const countryCard = document.createElement('div')
                        const countryCardImage = document.createElement('img')
                        countryCard.innerHTML = country.name
                        countryCardImage.src = country.flag
                        document.getElementById('feed').appendChild(countryCard)
                    })
                }
            }
            xhr.send()
       } 
    </script> 
      

Because country.name is an object.You should use an string to set the innerHTML of element.因为country.name是 object。您应该使用字符串来设置元素的 innerHTML。 According to your response data structure, you can use it like this:根据您的响应数据结构,您可以像这样使用它:

countryCard.innerHTML = country.name.common; // or any other property

I finally figured out how to display flags, here is the source:我终于想出了如何显示标志,这是源代码:

                function showCountries(){
                let xhr = new XMLHttpRequest()
                    xhr.open('GET', 'https://restcountries.com/v3.1/all', true)
                    xhr.onload = function(){
                    if(xhr.status == 200){
                    console.log('success')
                    let countries = JSON.parse(this.response)
                    countries.forEach(country=>{
                        const countryCard = document.createElement('div')
                        const countryCardImage = document.createElement('div')// must be div to display small image
                        countryCard.innerHTML = country.name.common

                        countryCardImage.innerHTML = country.flag
                        console.log(country.flag)

                        document.getElementById('feed').appendChild(countryCard)
                        document.getElementById('feed').appendChild(countryCardImage) //this is it!
                    })
                }
            }

暂无
暂无

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

相关问题 我正在从 API 请求数据。 目前,我以对象数组的形式获取数据,但无法在浏览器上呈现输出 - I am requesting data from an API. Currently, I am getting data as an array of objects but I'm not able to render output on the browser 我应该怎么做才能从 API 调用中呈现数组对象值的总和 - What should I do to render sum of array objects values from API call 我向Twitter API提出了请求。 如何从PHP脚本获得对JavaScript的JSON响应? - I made a request to the Twitter API. How can I get the JSON response from my PHP script to my JavaScript? 我可以只渲染我的页面一次。 刷新页面后,我收到一个错误,因为无法从 api 中找到数据。请让我知道我的错误 - I can render my page just once. After refreshing the page, i am getting an error as cant find data from api. Please let me know my mistake 如何从API请求中获取JSON到我的页面的javascript中? - How do I get the JSON from an API request into my page's javascript? 我无法从此 API 获取数据。 这是代码 - I am unable to fetch data from this API. Here is the code 使用电子邮件和密码登录后,我需要将电子邮件传递到另一个页面,以便从 API 检索用户数据。 (反应) - After I login with email and password, I need to pass my email to the further page for retrieve the user data from the API. (React) 尝试通过crunchbase API提取JSON数据。 我究竟做错了什么? - Trying to pull JSON data via the crunchbase API. What am I doing wrong? 如何使用JavaScript显示RESTful API调用返回的JSON - How do I display returned JSON from RESTful API call with JavaScript 为什么我尝试使用 fetch API 从 html 页面获取数据时出现 400 错误? - Why do I get a 400 error trying to get data from an html page using fetch API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM