How to use AJAX with WordPress

Here is the simple tutorial to do so.

On Client Side (JAVASCRIPT)

<script>
$(document).on('click', '.remove', function(){
let url = "<?=admin_url('admin-ajax.php')?>";
$.post(url,
{
action : "ajax_test", 
id : "1",
},
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
})
</script>

On Server Side: (In Plugin or function.php file)

The wp_ajax_{action} hook only fires for logged in users. For logged-out users the action wp_ajax_nopriv_{action} is triggered on an ajax request – so you need to hook into that as well.

<?php

add_action("wp_ajax_ajax_test",  "ajaxTest");
add_action("wp_ajax_nopriv_ajax_test",  "ajaxTest"); function ajaxTest(){ echo "I am a simple ajax"; } ?>