Unable to use $wpdb in WordPress

You should use the default way the WP recommend to handle ajax calls. This will save you from lots of pitfalls and bad practices.

add_action( 'wp_ajax_nopriv_****', 'your_custom_function' );
add_action( 'wp_ajax_****', 'your_custom_function' );

function your_custom_function() {

}

JS part

jQuery.ajax({
    type: "post",
    dataType: "json",
    url: ajax_url,// need the admin_url('admin-ajax.php')
    data: formData, // your data
    success: function(msg){

    }
});

Additional context