How to run 2 ajax request with 2 different actions using the same data in jquery $.ajax

Viewed 26

i have a function that contain 2 $.ajax functions with different actions but using same data i put these data inside an object, how to add different action to every $.ajax function

it's mandatory to use same data inside an object

var request_data = {
            nonce: ajax_object.nonce, 
            category_id: cat_ids,
            brand_name: brand_name_value,
            orderby_meta_key: orderby_meta_key,
            orderby: orderby,
            order_type: order_type,
            shop_view: shop_view
        } 

    function ajax_filter(){

// firest ajax function       
        $.ajax( {
            url: ajax_object.ajax_url,
            type: 'post',
            action:'shop_filter',   ------> //not working like this
            data: request_data,
            success: function(feedback) {
            }
        })
            
// second ajax function
       $.ajax( {
            url: ajax_object.ajax_url,
            type: 'post',
            action: 'update_another_function',   ----> //not working like this
            cache: true,
            data: request_data,
            success: function(feedback) {
            },
        })

    }
1 Answers

There is no action parameter in jQuery.ajax, should your action be in the request data?

function ajax_filter(){
   request_data.action = 'shop_filter';
// firest ajax function       
    $.ajax( {
        url: ajax_object.ajax_url,
        type: 'post',
        data: request_data,
        success: function(feedback) {
        }
    })
   request_data.action = 'update_another_function';     
// second ajax function
   $.ajax( {
        url: ajax_object.ajax_url,
        type: 'post',
        cache: true,
        data: request_data,
        success: function(feedback) {
        },
    })

}
Related