0

I am using ajax in wordpress theme,but ir response -1,why? how to correct this?

function videospage(cnt)
{
    var pageno=cnt;

    jQuery.ajax({  
        type: "POST",  
        url: "<?php echo admin_url('admin-ajax.php'); ?>",  
        data: {  
            action: "myajaxpagination",  
            pageno: pageno,  
        },  
        success: function(datas){  
            alert(datas);  
        },  
        error: function(MLHttpRequest, textStatus, errorThrown){  
            alert(errorThrown);  
        }  
    });  

}

add_action('wp_ajax_myajaxpagination', 'my_action_pagination');  
function my_action_pagination()
{
    $pageno=$_REQUEST['pageno'];
    echo $pageno;
    die();
}

1 Answer 1

2

Pay attention that your ajax request will work only for logged in users. If you want to make it working for everybody, you have to add one more action wp_ajax_nopriv_myajaxpagination with the same hook:

function videospage(cnt)
{
    var pageno=cnt;

    jQuery.ajax({  
        type: "POST",  
        url: "<?php echo admin_url('admin-ajax.php'); ?>",  
        data: {  
            action: "myajaxpagination",  
            pageno: pageno,  
        },  
        success: function(datas){  
            alert(datas);  
        },  
        error: function(MLHttpRequest, textStatus, errorThrown){  
            alert(errorThrown);  
        }  
    });  

}

add_action('wp_ajax_myajaxpagination', 'my_action_pagination');  
add_action('wp_ajax_nopriv_myajaxpagination', 'my_action_pagination');
function my_action_pagination()
{
    $pageno=$_REQUEST['pageno'];
    echo $pageno;
    die();
}

Read more about AJAX in WordPress.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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