How to send data from JavaScript to the controller in CodeIgniter 4 using ajax?

To perform this task we need to define 3 things in Ajax i.e. “URL”, “type” and “data” after define these 3 things we can check its output in 2 functions that if data has been sent successfully than its response catch in “success” function while if any error occur than to handle it a function called “error”. step 1 create write following code in script tag or in js file.
$.ajax({

    url : "write your url here",
    type: 'POST', //define type GET or POST
    data: {"data":data},
    success: function(){
      alert("success");
    },
    error: function(){
      alert("error");
    }


  });
step 2 Now in php code go to controller and function where you want to get the above data which is defined in data variable. And inside it get it by using below type.
$data = $_GET["data"]; //for get method
$data = $_POST["data"]; // for post method

How to send data from JavaScript to the controller in CodeIgniter 4 using ajax?expoundsofttechsolution

How to send data from JavaScript to the controller in CodeIgniter 4 using ajax?