how to set beforsend option in wordpress ajax methods

How do I set the beforesend option in WordPress AJAX? I want to show loading option.

This is my code:

function showmyvideos() { 
    var datas="data";
    var data = {
        action: 'my_vids',
        link: datas  
    }; 
    ajaxurl= "http://localhost/wordpress/wp-admin/admin-ajax.php";
    jQuery.post(ajaxurl, data,function(response) {

    });

}

Related posts

Leave a Reply

2 comments

  1. For posterity.

    You can still use $.ajax() without having to use $.ajaxSeteup(). All you need to specify is the type and make sure it is set to POST.

    $.ajax({
    
        url: localized_script.ajax_url,  /* Admin ajax url from localized script */
        type: 'POST',  /* Important */
        data: data_object,  /* Data object including 'action' and all post variables */
        beforeSend: function() {
            alert('Before Send');
        },
        success : function(response) {
            alert(response);  /* Response from ajax function */
        },
        complete: function() {
            alert('Complete');
        }
    });
    

    As you can see, you can set different functions/events for beforeSend, success and complete.