How to set Ajax callback from jquery on node edit form?
Listening to your suggestion I did this. 
Now this is my_module.module file:

    /**
 * Implements hook_form_FORM_ID_alter().
 */
function my_module_form_node_test_form_alter(&amp;$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

  $form['field_test1']['widget']['#ajax'] = [
    'event' =&gt; 'change',
    'callback' =&gt; 'my_module_myAjaxCallback',
    'disable-refocus' =&gt; FALSE,
    // Or TRUE to prevent re-focusing on the triggering element.
    'progress' =&gt; [
      'type' =&gt; 'throbber',
      'message' =&gt; t('Verifying entry...'),
    ]
  ];

  $form['#attached']['library'][] = 'my_module/trytest'; //form.js
}

/**
 * An Ajax callback.
 */
function my_module_myAjaxCallback(array &amp;$form, FormStateInterface $form_state) {
  $db = new mysqli('host', 'user', 'password', "db");
  mysqli_set_charset($db, "UTF8");
  // Check connection
  if ($db-&gt;connect_error) {
    die("Connection failed: " . $db-&gt;connect_error);
  }
  else{
    echo "Connected successfully";
  }

  if(\Drupal::request()-&gt;request-&gt;get('field_test1')){
    $out = array();
    $sql = "SELECT * FROM `car_type`";
    $res = $db-&gt;query($sql) or die(mysqli_error($db));
    while($row = mysqli_fetch_object($res)){
      $out[] = array(
        'id' =&gt; $row-&gt;id_car_type,
        'name' =&gt; $row-&gt;name
      );
    }
    echo json_encode($out);
  }
}


Obviously it doesn't work because I don't know if it is correct to insert the data of the connection to the DB in this file and even if the code to return the query result to the file form.js. is correct.