Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

364
Views
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. For couple of fields i.e 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].

Do i have to use JSON.stringify?

Any help appreciated.

Thanks, Ajeet

about 4 years ago ยท Juan Pablo Isaza
3 answers
Answer question

0

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

about 4 years ago ยท Juan Pablo Isaza Report

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.

Try to pass data.flags.png or data.flags.svg for the src attribute and it should work.

about 4 years ago ยท Juan Pablo Isaza Report

0

You need to use proper notation to get values out from 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);
});
about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!