How to set Ajax callback from jquery on node edit form? I must to create a dependent Fields with three fields in my content type. This fields are select list, but the options for each Field will be prepopulate from a sql query on external DB.

So, this is my form.js file:

 (function ($, Drupal) {
  'use strict';

  Drupal.behaviors.customTest = {
    attach: function (context, settings) {
      getTypes();

      function getTypes() {
        $('#edit-field-test1 option').not(':first').remove();
        alert("test")
        var request = $.ajax({
          url: "../../modules/custom/my_module/script/ajax.php",
          method: "POST",
          data: 'field_test1 =1',
          dataType: "json"
        });

        request.done(function(data){
          var opt = '';

          for(var i = 0, max = data.length; i &lt; max; i++){
            opt += '&lt;option value="'+data[i].id+'"&gt;'+data[i].name+'&lt;/option&gt;';
          }

          $('#edit-field-test1').append(opt);
        });

        request.fail(function(jqXHR, textStatus){
          alert('Request failed: '+textStatus);
        });
      }
    }
  };
}(jQuery, Drupal));


And this is my ajax.php file:

    &lt;?php
include '../db/bd.php'; &lt;!--This file With host, user, password external DB--&gt;

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);
}


So, form.js file is declarated in my_module.libraries.yml, but ajax.php? where should i declare it? how do i use it?
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.