Eric K
Eric K

Reputation: 726

Ajax with WordPress Functions.php always returns 0

I know this question has been asked a lot, so I apologize for any redundancy. However, simply can not seem to make this return a value other than 0. Below is the code that I have included at the bottom of the functions.php within the Twenty-Twelve theme:

add_filter( 'views_edit-destination', 'so_13813805_add_button_to_views' );
function so_13813805_add_button_to_views( $views )
{
    $views['my-button'] = '<button id="update-dest-cache" type="button" class="button"             title="Update Destinations Cache" style="margin:5px" onclick="updateDestCache()">Update Destinations Cache</button>';
    $views['my-button'] .= '
        <script type="text/javascript" >
        function updateDestCache() {
            jQuery.ajax({
                type: "POST",
                url: ajaxurl,
                dataType: "json",
                action: "testAjaxFunction",
                success: function(response){ 
                    alert("Got this from the server: " + response);
                },
                error: function(MLHttpRequest, textStatus, errorThrown){  
                    alert("There was an error: " + errorThrown);  
                },
                timeout: 60000
            });    
        };
        </script>
    ';
    return $views;
}
function testAjax(){
    echo "ANYTHING!!!!";
    die();
}
add_action('wp_ajax_testAjaxFunction', 'testAjax');
add_action('wp_ajax_nopriv_testAjaxFunction', 'testAjax' );

This code adds a button to the Edit list of a custom post type, and then runs the function upon click. The button shows up, the function runs, the ajax function is called, but 0 is always the response.

Any thoughts on why this continues to occur?

Upvotes: 1

Views: 7787

Answers (2)

Eric K
Eric K

Reputation: 726

I had my javascript written incorrectly.

function updateDestCache() {
   jQuery.ajax({
      type: "POST",
      url: ajaxurl, 
      data: { action: "testAjaxFunction" }, <---- THIS LINE WAS CHANGED
      success: function(response){ 
         alert("Got this from the server: " + response);
      },
      error: function(MLHttpRequest, textStatus, errorThrown){  
         alert("There was an error: " + errorThrown);  
      },
      timeout: 60000
   });    
};

Upvotes: 2

RRikesh
RRikesh

Reputation: 14411

The ajaxurl is only defined in your dashboard. If you're using it on the front-end, you should declare it:

$admin_url = admin_url('admin-ajax.php');
$views['my-button'] .= '
        <script type="text/javascript" >
        function updateDestCache() {
            jQuery.ajax({
                type: "POST",
                url: '. $admin_url .', 
                dataType: "json",
                action: "testAjaxFunction",
                success: function(response){ 
                    alert("Got this from the server: " + response);
                },
                error: function(MLHttpRequest, textStatus, errorThrown){  
                    alert("There was an error: " + errorThrown);  
                },
                timeout: 60000
            });    
        };
        </script>
    ';

Upvotes: 0

Related Questions