title: Loading JSON data with button click and textbox
body: <p>So I'm learning javascript and I'm trying to create a movie database where a user enters a movie id in a textbox and clicks the &quot;load a movie button&quot; which loads information about the movie with that specific id.</p>
  <p>I was given a json file which contains elements of a movie such as the id, a title, release date, etc. I'm having trouble adding a click handler to my movie request button that creates an XMLHttpRequest object and submits a &quot;GET&quot; request for a specific movie. If this request is successful, the response text available in my callback function will be a JSON string containing the movie’s information in the format below. So in the console it would print out:</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;Title&quot;: &quot;Guardians of the Galaxy Vol. 2&quot;,
    &quot;Year&quot;: &quot;2017&quot;,
    &quot;Rated&quot;: &quot;PG-13&quot;,
    &quot;Released&quot;: &quot;05 May 2017&quot;,
    &quot;Runtime&quot;: &quot;136 min&quot;,
    &quot;imdbID&quot;: &quot;tt3896198&quot;,
    &quot;Response&quot;: &quot;True&quot;
  }
  </code></pre>
  <p>and it should just print the title and runtime as regular text in the loaded html page as that's what I'm requesting in the javascript file.</p>
  <p>This json file only contains 1 movie for simplicity, but my original json file will contain information for thousands of movies, so when I'm searching a specific movie id, it should only load information for the one movie instead of all the movies in the json file. So if I enter &quot;tt3896198&quot; in the textbox, this id should load the data for the movie &quot;Guardians of the Galaxy Vol. 2&quot; as this is the id that matches this movie, as shown in the json file.</p>
  <p>I tried looking up how to print specific information from the json file using XMLHttpRequests and JSON parsing but the problem is the examples I see online are able to access the variables in their json file by doing something like &quot;movie.title&quot; because their variables are lowercase. All my variables in my JSON file appear to be uppercase, and I don't think doing movie.Title is allowed.</p>
  <p>Additionally, I was wondering how I would load a movie based on what id the user enters? There are 1000s of movies with unique ids, and I'm not sure how to load only one movie whose id matches the id entered in the textbox.</p>
  <p>Right now my page just shows a textbox and a button, but the button doesn't work. Any help would be appreciated, I'm having trouble mostly with just getting the data from the json file to show up on the console as well as the HTML page when I click the button. If anyone could help or guide me in getting the movie id element to work, that'd be appreciated too!!</p>
  <p>movie.html:</p>
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Movies page&lt;/title&gt;
      &lt;/head&gt;
  
      &lt;body onload=&quot;init()&quot;&gt;
          &lt;div&gt;
              &lt;input type=&quot;text&quot; placeholder=&quot;Search&quot; name=&quot;textbox&quot;&gt;
              &lt;button type=&quot;button&quot; id=&quot;retrieveMovies&quot;&gt;Load a movie&lt;/button&gt;
          &lt;/div&gt;
          &lt;br /&gt;
          &lt;div id=&quot;moviediv&quot;&gt;
  
          &lt;/div&gt;
  
      &lt;script src=&quot;movies.js&quot;&gt;&lt;/script&gt;
      &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  <p>movie.js:</p>
  <pre><code>let movies = [];
  
  function init(){
      document.getElementById(&quot;retrieveMovies&quot;).onclick = loadMovies;
      loadMovies();
  }
  
  function loadMovies(){
      let xhttp = new XMLHttpRequest();
  
      xhttp.onreadystatechange = function() {
          if (this.readyState == 4 &amp;&amp; this.status == 200) {
              let responseObject = JSON.parse(xhttp.responseText);
              movies = responseObject.results;
              render();
          }
      };
  
      xhttp.open(&quot;GET&quot;, &quot;movie.json&quot;, true);
      xhttp.send();
  }
  
  function render(){
      let content = &quot;&quot;;
      movies.forEach(movie =&gt;{
          content += `
              &lt;div&gt;
                  &lt;p&gt; Movie: ${movie.Title}&lt;/p&gt;
                  &lt;br/&gt;
                  &lt;p&gt; Runtime: ${movie.Runtime}&lt;/p&gt;
              &lt;/div&gt;
          `
      })
      document.getElementById(&quot;moviediv&quot;).innerHTML = content;
  }
  </code></pre>
  <p>movie.json:</p>
  <pre><code>{&quot;Title&quot;:&quot;Guardians of the Galaxy Vol. 2&quot;,&quot;Year&quot;:&quot;2017&quot;,&quot;Rated&quot;:&quot;PG-13&quot;,&quot;Released&quot;:&quot;05 May 2017&quot;,&quot;Runtime&quot;:&quot;136 min&quot;,&quot;imdbID&quot;:&quot;tt3896198&quot;,&quot;Response&quot;:&quot;True&quot;}
  </code></pre>
  <p>UPDATE: So I attempted to create a server to run the application instead of directly opening the html file from my directory.</p>
  <p>server.js:</p>
  <pre><code>const http = require('http');
  const fs = require('fs');
  
  const server = http.createServer(function (request, response) {
      if(request.method === &quot;GET&quot; &amp;&amp; (request.url === &quot;/index.html&quot; || request.url === &quot;/&quot;)){
          fs.readFile('index.html', 'utf8', function(err, contents) {
              if(err){
                  response.writeHead(500, { &quot;Content-Type&quot;: &quot;text/html&quot;})
                  response.end();
                  return;
              }
              response.writeHead(200, { &quot;Content-Type&quot;: &quot;text/html&quot;})
              response.end(contents);
          });
      }else if(request.method === &quot;GET&quot; &amp;&amp; request.url === &quot;/style.css&quot;){
          fs.readFile('style.css', 'utf8', function(err, contents) {
              if(err){
                  response.writeHead(500, { &quot;Content-Type&quot;: &quot;text/css&quot;})
                  response.end();
                  return;
              }
              response.writeHead(200, { &quot;Content-Type&quot;: &quot;text/css&quot;})
              response.end(contents);
          });
      }else if(request.method === &quot;GET&quot; &amp;&amp; request.url === &quot;/movies.js&quot;){
          fs.readFile('movies.js', 'utf8', function(err, contents) {
              if(err){
                  response.writeHead(500, { &quot;Content-Type&quot;: &quot;application/javascript&quot;})
                  response.end();
                  return;
              }
              response.writeHead(200, { &quot;Content-Type&quot;: &quot;application/javascript&quot;})
              response.end(contents);
          });
      }
  });
  
  server.listen(3000);
  console.log('Server running at http://127.0.0.1:3000/');
  </code></pre>
  <p>I type localhost:3000 in my google browser and it leads me to the html page. While this resolved the errors I had previously, clicking on the add button still does nothing it seems.</p>