Q:

Call Php controller function with AJAX Codeigniter

I , im working with Ajax and Codeigniter to call function client-server
the php
public function mainViewClean() {

        unset($_SESSION[$this::jsondevices]);
        unset($_SESSION[$this::jsontags]);
       return "Ready";
    }
//route $route['cleantags']       = 'user/mainViewClean';

And ajax:
<script type="text/javascript">
    $(document).ready(function(){
        $("#btn_recargar").button().click(function(){

            //window.location.href = "<?= base_url('home')?>";
             $.ajax({
                type:'POST',
                url:'<?php echo base_url("cleantags"); ?>',
                data:{'id':100},
                success:function(data){
                 //window.location.href = "<?= base_url('home')?>";
                    alert(data);
                }
            });

   });
});
</script>

The function excuse good , but javascript don't show any data , what im doing wrong?

A:

Change return into :
echo "Ready";

If you're sending an array, at server side you need to json_encode, example :
// encode array into json string format
echo json_encode( array( 'name' => 'Osman' ) );

And in Js, you have 2 options, the 1st solution is :
success : function ( data ) {
   // data now is coming in this form { "name" : "osman" }
   // as the string data is coming from server-side
   // you must parse it back into Javascript object
   var newData = JSON.parse( data );
}

And the 2nd option is, add dataType properties inside ajax properties like following :
$.ajax({
  ...
  dataType : 'json', // with this, no need to write JSON.parse()
  ...
});

