Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i want to call a function using wordpress ajax and i have done a code for that also. but when i fire a ajax call it always retrun -1. i dont know what is the problem can any body help me to solve.

Here is my code

function.php

 function my_AJAX_processing_function(){
   //echo  $getLetter = $_GET['post_id'];
   print_r($_GET);
   exit;
    global $wpdb;
    global $post;

    $result = $wpdb->get_results('SELECT * FROM wp_posts where post_title LIKE "'.$getLetter.'%" AND post_status = "publish" AND post_type="post"');
    while($row = $result)
    {
      echo '<a href="' . $row["guid"] . '">' . $row['post_title'] . '</a><br>';
    }
    die();
}
add_action('wp_ajax_nopriv_download_track_resources', 'my_AJAX_processing_function');

footer.php

    <script type="text/javascript">
jQuery(function($) {
    $(document).ready(function() {
        $(".downloadtrack").bind('click', function(){
            console.log(this.name);
            var siteurl = '<?php echo esc_url( home_url( '/' ) ); ?>';
            alert(siteurl);
            var val = this.name;
            alert(val);
            var data = {action: 'download_track_resources', post_id: val};
            $.ajax({
                 url:siteurl+'wp-admin/admin-ajax.php',
                 success:function(data){
                   alert(data);
                 }
            });
        });
    });
});
</script>

Html code

<td height="25"><a href="javascript:void(0);" title="Download <?php the_title(); ?>" class="downloadtrack" name="<?php echo $post->ID;?>"><?php the_title(); ?></a></td>

Thanks

share|improve this question

1 Answer 1

up vote 2 down vote accepted

The answer is quite simple - you are not giving admin-ajax what it is expecting.

Please refer to the source code http://core.trac.wordpress.org/browser/tags/3.2.1/wp-admin/admin-ajax.php#L17

if ( ! isset( $_REQUEST['action'] ) )
    die('-1');

So make sure you are giving it an action (which you are not). Then, a little lower there is:

if ( ! $id )
    die('-1');

So make sure you are logged in, and all the other prerequisites AJAX in WordPress Plugins require to function.

For more information check the AJAX Codex entry.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.