0

i'm trying to develop a new API, but i'm meeting some problems with Javascript:

My JS/HTML CODE:

const express = require('express');
const app = express();
const PORT = 3000;


submit.onclick = function()
{
    
    let email = document.getElementById('email').value;
    let api1 = 'https://disify.com/api/email/'+email;

    // Start check on API1
      var xmlHttp = new XMLHttpRequest();
      xmlHttp.open("GET", api1, true); // false for synchronous request
      xmlHttp.onload = function()
      {
        if (xmlHttp.readyState === 4) //if ready
        {
          if (xmlHttp.status === 200) //if server response
         {
            console.log(xmlHttp.responseText); //write in log the response
            var data = JSON.parse(this,response);
            alert(data.whitelist);
            var status = data.whitelist;
            //var status = data.main.whitelist;
            console.log(data.whitelist);
            console.log(data.domain);
          }
          else
         {
            console.error(xmlHttp.statusText);
          }
        }
      }
      xmlHttp.send();
      ris = document.getElementById('ris').innerHTML = "La mail: " + email + " è attualmente " +status;
    };
<html>
<head>
  <meta charset="utf-8">
  <title>[Alpha] My Personal API v1.0</title>
  <!-- <script type="text/javascript" src="server.js"></script> -->
</head>
<body>
  <div style="text-align:center">
    <h1>Mail Checker API - v1.0</h1>
  </div>
<div style="text-align:center">
<h2>Immetti una mail:</h2>
<input type="email" id="email" name="email"></input>
<br><br>
<button id="submit">Cerca</button>
<p id="ris"></p>
</div>
<script src="server.js"></script>
</body>
</html>

As you can see, i have a button called "submit", that is linked in the javascript code by event "onclick", but the console show me this error:

Terminal-error-image

Barmar
  • 741,623
  • 53
  • 500
  • 612
Zenek
  • 60
  • 2
  • 12
  • 1
    Event listeners run on the client, not the server. – Barmar Mar 07 '20 at 15:01
  • @Barman it's an API of API. I call the api on https://disify.com. i don't know why "onclick" event doesn't work – Zenek Mar 07 '20 at 15:20
  • @Zenek — Because you are trying to assign it in the JavaScript program you are running in Node.js and not the entirely different JavaScript program running on the browser. – Quentin Mar 07 '20 at 15:34

1 Answers1

-1

First you need to define 'submit' variable then you should attach the 'click' event.

let submit = document.getElementById('submit');
submit.onclick =  function()
{

};
  • While this is best practice, in a browser an element with an `id` will get a matching global variable automatically. The problem here is that the code is not running in a browser. – Quentin Mar 07 '20 at 15:33
  • TypeError: document.getElementById is not a function – Zenek Mar 07 '20 at 15:42
  • @Quentin it's running in a nodejs server and i am working on chrome – Zenek Mar 07 '20 at 15:43
  • @Zenek — That. Is. The. Problem. You are submitting from Chrome not on Node.js. Therefore it needs to be in the program that runs in Chrome and not the program that runs in Node.js. – Quentin Mar 07 '20 at 15:47
  • @Quentin so? How can i fix it ? – Zenek Mar 07 '20 at 16:08
  • @Zenek https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script – Quentin Mar 07 '20 at 19:03