wp_localize_script, just like wp_enqueue_script and wp_register_script, should be called from within a hook.
Usually you register you scripts on the ‘init’ hook, and enqueue them on the ‘wp_enqueue_scripts’ hook.
Assuming ‘like_post’ is an already registered script, here is a working localization example:
add_action('wp_enqueue_scripts', 'enqueue_theme_scripts');
function enqueue_theme_scripts()
{
$params['url'] = admin_url('admin-ajax.php');
$params['nonce'] = wp_create_nonce('ajax-nonce');
wp_enqueue_script('like_post');
wp_localize_script('like_post', 'ajax_var', $params);
}
Hope this helps.