AJAX and custom pages

I think there’s a misunderstanding here about how browser page requests work, and how PHP loads and runs WordPress

When you make a request to WordPress, PHP loads WordPress and processes the page, then it’s all cleaned up and thrown away. Unlike other platforms such as Java or Python, there is no continuously running program in the background. Each request loads WordPress from scratch. For things to carry over to the next request you have to save things in the database or the filesystem.

The same is true of browser requests. Your browser sends a HTTP request to the server, and it generates a HTML document and sends it back.

Which leads us to your code. An AJAX request is another request, sent by the browser, that loads WordPress from scratch. It can’t go back in time to modify the scripts and styles in the previous request to add the media library.

For enquing scripts to work, you need to do it on the same request that generates the page, and that page needs to call wp_head. So, it can never be done on an AJAX request as there is no header to print the tags out in.

TLDR: You can’t use AJAX queries to enqueue scripts and styles, that’s not how it works, and you can’t use AJAX to change what happened in PHP on a previous request

If you want to enqueue those things on your page, you need to do it in your theme, on the appropriate hook, not in an admin AJAX handler. There’s an example of this in the tutorial you’re following that can be copy pasted as is, just keep in mind all the tutorials you’ll find will assume you’re wanting to do this in WP Admin, not the frontend