'Why does ajax.php script duplicate in wordpress header

I have this add_action to load my ajax var in the header of wordpress, but for some reason it duplicates the call. I have another method setup too and no matter what it will duplicate the var for ajax. I know you can run the php call in the ajax script to avoid using this method but I want to keep my js in a js file and not place it in the footer or header.

add_action('wp_head', array($this, 'my_fts_ajaxurl'));

function my_fts_ajaxurl() {
    wp_enqueue_script('jquery'); ?>
    <script type="text/javascript">
        var myAjaxFTS = '<?php echo admin_url('admin-ajax.php'); ?>';
    </script>
    <?php
}

and this is what gets returned in the header.

<script type="text/javascript">var myAjaxFTS='http://www.example.net/betablog/wp-admin/admin-ajax.php';</script>
<script type="text/javascript">var myAjaxFTS='http://www.example.net/betablog/wp-admin/admin-ajax.php';</script>

I also have another method in place that uses the wordpress method. I'm using a class.

add_action('init', array($this, 'fts_clear_cache_script'));
add_action('wp_ajax_fts_clear_cache_ajax', array($this, 'fts_clear_cache_ajax'));

function fts_clear_cache_script() {
    wp_enqueue_script('fts_clear_cache_script', plugins_url('feed-them-social/admin/js/developer-admin.js'), array('jquery'));
    wp_localize_script('fts_clear_cache_script', 'ftsAjax', array('ajaxurl' => admin_url('admin-ajax.php')));
    wp_enqueue_script('fts_clear_cache_script');
}

And this is what gets returned in the header.

<script type="text/javascript">
/* <![CDATA[ */
var ftsAjax = {"ajaxurl":"http:\/\/www.example.net\/betablog\/wp-
admin\/admin-ajax.php"};
var ftsAjax = {"ajaxurl":"http:\/\/www.example.net\/betablog\/wp-
admin\/admin-ajax.php"};
/* ]]> */
</script>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source