How to request admin-ajax.php correctly when wordpress URL and site URL are different?

admin_url() should be used.

However for some websites this request gives error 404 in front-end

admin_url('admin-ajax.php') should point to an actual file (admin-ajax.php), as such WordPress won’t pass it through index.php, and so it shouldn’t lead to the theme’s 404 page. (Unless the file has been deleted).

… unless you have a .htaccess file which is redirecting the user away from wp-admin (for example) (a recent client of mine had been doing this to whitelist IP addresses that could reach wp-admin).