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

167
Views
How to get values from JSON XMLHttpRequest?

I am new to web development, and I am using a XMLHttpRequest() in JavaScript to get data from an api. I am trying to create variables from the data but am getting an error when I try to do the following. Does anyone know what is wrong with the line var data1 = data["data1"];?

<script>
    const Http = new XMLHttpRequest();
    const url = "www.mytestapi.com/response.json";
    Http.open("GET", url);
    Http.send();
                    
    Http.onreadystatechange = (e) => {
        var json = JSON.parse(Http.responseText)
        var data = json.Data
        var data1 = data["data1"]; //issue caused here
    }
<script>  
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you don't need to parse response data, data is parsed already , try this


   const xhr = new XMLHttpRequest();
    const url = "www.mytestapi.com/response.json";
    xhr.open('GET', url);
    xhr.responseType = 'json';
    xhr.onload = () => {
        console.log("load - "+ JSON.stringify(xhr.response));
        var data = xhr.response;
        var data1 = data["data1"]
    }
     xhr.onerror = () => {
        console.log("error status - " + xhr.status);
    }
    xhr.send()
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!