简体   繁体   English

使用国家 API 通过 XMLHTTPRequest 获取国家数据 gettting [object object]error

[英]Fetching countries data through XMLHTTPRequest using the countries API gettting [object object]error

Fetching the data through this API https://restcountries.com/v3.1/name/portugal .通过此 API https://restcountries.com/v3.1/name/portugal获取数据。 For couple of fields ie flag,name,currency getting [object object]对于几个字段,即标志、名称、货币获取 [对象对象]

XMLHTTPRequest

const request = new XMLHttpRequest();
// Here we need URL for AJAX calls
//request is an object
// Type of Request - GET
request.open('GET','https://restcountries.com/v3.1/name/portugal');
// open the request
//we have to send this request to the url to fetch the data
request.send();

//We need request call back on the load. callback function
request.addEventListener('load',function() {

// converting json file to normal text.

const [data] = JSON.parse(this.responseText);
console.log(data);

const html=`
<article class="country">
<img class="country__img" src="${data.flags}"/>
console.log(data.flags);
<div class="country__data">
  <h3 class="country__name">${data.name}</h3>
  <h4 class="country__region">${data.region}</h4>
  <p class="country__row"><span>👫</span>${data.population}</p>
  <p class="country__row"><span>🗣️</span>${data.languages}</p>
  <p class="country__row"><span>💰</span>${data.currencies}</p>
</div>
</article>
`;

For Flag image getting Cannot GET /[object%20Object] other fields too [object%20Object].对于 Flag 图像获取 Cannot GET /[object%20Object] 其他字段也是 [object%20Object]。

Do i have to use JSON.stringify?我必须使用 JSON.stringify 吗?

Any help appreciated.任何帮助表示赞赏。

Thanks, Ajeet谢谢,阿吉特

I think you need to do this:我认为你需要这样做:

const data = JSON.parse(this.responseText);
console.log(data);
console.log(data[0].population); etc

ie remove the square brackets from const [data] and get using index 0即从 const [data] 中删除方括号并使用索引 0

The data.flags property you are using for src attribute is an object itself, not a simple URL, since it contains URLs for png and svg formats.您用于src属性的data.flags属性是一个对象本身,而不是一个简单的 URL,因为它包含pngsvg格式的 URL。

Try to pass data.flags.png or data.flags.svg for the src attribute and it should work.尝试为src属性传递data.flags.pngdata.flags.svg ,它应该可以工作。

You need to use proper notation to get values out from JSON.您需要使用正确的符号从 JSON 中获取值。 You can refer below code to get more understandings.您可以参考下面的代码以获得更多的理解。

request.addEventListener('load', function () {
    // converting json file to normal text.
    
    const [data] = JSON.parse(this.responseText);
    console.log(data);
    
    const lang = Object.values(data.languages).join(',');
    const currencies = Object.values(data.currencies).map((c) => c.name).join(',');
    
    const html = `
    <article class="country">
    <img class="country__img" src="${data.flags.png}"/>
    <div class="country__data">
        <h3 class="country__name">${data.name.common}</h3>
        <h4 class="country__region">${data.region}</h4>
        <p class="country__row"><span>👫</span>${data.population}</p>
        <p class="country__row"><span>🗣️</span>${lang}</p>
        <p class="country__row"><span>💰</span>${currencies}</p>
    </div>
    </article>
    `;
    const divEl = document.createElement('div');
    divEl.innerHTML = html;
    
    document.body.appendChild(divEl);
    console.log(html);
});

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

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