Push value in an empty array from checkbox selection

Push value in an empty array from checkbox selection

I am retriving some JSON data from an API using Jquery AJAX and I want to change the url dynamicly on checkbox selection. I have four checkbox and I want to store checkbox value to an globle array. When I put console.log(category) or ajax code inside function its working but when I puth the code outsite the function its not working.

  1. var category = [];
    $(".ckbox").click(function() {
          if($(this).is(":checked")) {
            category.push($(this).val());
          } else {
            var x = category.indexOf($(this).val());
            category.splice(x, 1);
          } 
        });

        $.ajax({
                dataType: 'json',
                type: 'GET',
                data: {
                  category: category, //I wnat to change this
                  city: 'PUNE',
                  begin: '0',
                  limit: '200',
                },
                traditional: true,
                success: function(response){
                  // console.log(response);
                },
                error: function(err){
                  alert("something went wrong.");
                  console.log(err);
                }
            });