Custom HTML form using PHP – help with ajax/username validation

This is how I got AJAX working for me.

  1. I digested this tutorial. https://codex.wordpress.org/AJAX_in_Plugins

  2. I created an external JS file and enqueued after jQuery because of its dependency on jQuery.

  3. I created a PHP function to respond to the AJAX call by echoing what was sent.

  4. When that worked, I made the PHP function send the data I needed.

I think what you have should work. If the let declaration gives you an error, try it with var instead. This is stupid, I know, but if var doesn’t work then the error is in the right side of the expression.

I would replace url: '<WP_AJAX_URL_HERE>' with ajaxurl and see if that helps.