0

I have a simple app in codeigniter. In my view I have ajax POST request to the controller, but I don't receive response data. I checked in the chrome dev tool in network tab the response and the message is: No response data available for this request : My view:

    <table class="table table-striped table-bordered">
<thead>
<tr>

<td>First</td>
<td>Sec</td>
<td>Th</td>
<td>Th</td>
</tr>
</thead>
<tbody>
<?php 
if(isset($result))
{
foreach($result as $value)
{
    echo "<tr><td>". $value->cai . "</td><td>". $value->partner ."</td><td>". $value->spot ."</td><td><button type='button' id='".$value->cai."' class='btn btn-danger delete'>Delete</button></td></tr>";
 
}
}
?>
</tbody>
</table>
<?php echo $this->pagination_bootstrap->render(); ?>
<script>
$('.table').on('click','.delete',function(){
    
    var id=$(this).attr('id');
    var confirm_box=confirm('Sure?');
    if(confirm_box === true)
    {
        $.ajax({
        url: '<?php echo site_url('Tires_Crud/deleteCai'); ?>',
        type: 'POST',
        data: {
            key: id
        },
        dataType: 'json',
        success: function(data) {
            console.log(JSON.parse(data));
        }
    });
    
    }
});
</script>

My controller:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Tires_Crud extends MY_Controller
{

  
    

    public function deleteCai()
    {

        $status = array(
            "STATUS" => "true"
        );
        return  json_encode($status);
        }

}

In my config file CSRF protection is DISABLED! Because when its enabled the ajax request is not working. Thanks for your help in advance!

devax19
  • 7
  • 1
  • 4

1 Answers1

0

Some issues with this code.

#1: The way you're forming the ajax request. Where you have type: 'POST' should be method: 'POST' - but more than a typo, it's important that you understand these are referred to as the HTTP method - same way you'd specify that in a <form method='post' tag. If you don't provide a method, it's assumed to be GET.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

Also, because you specified dataType:'json' - jquery will deliver a ready to use parsed JSON object. No need for you to parse it yourself

$.ajax({
  url: '<?php echo site_url('Tires_Crud/deleteCai'); ?>',
  method: 'POST', // <-- change this from 'type' to 'method'
  data: {
        key: id
    },
    dataType: 'json',
    success: function(data) {
        // console.log(JSON.parse(data)); // <-- no need to parse - you have set dataType to be json, which means it will come to you already parsed
        console.log(data);
    })

#2 - In your PHP, you'd catch your key variable (you sent in ajax) with $_POST['key'] - though in codeigniter you get it like this:

// codeigniter 2.x and 3.x
$this->input->post('key');  

// codeigniter 4
use CodeIgniter\HTTP\IncomingRequest;
$request = service('request');
$request->getPost('key');

#3 - With ajax, you actually echo the response, rather than return it:

public function deleteCai(){
  $status = array(
    "STATUS" => "true"
  );
  echo  json_encode($status);
}
Kinglish
  • 23,358
  • 3
  • 22
  • 43
  • @devax19 - did this help? – Kinglish Jun 10 '21 at 02:24
  • @devax19 Thank you for your reply, but I still don't receive a response message. – devax19 Jun 10 '21 at 07:10
  • and you're sure your POST request is getting sent (you see in your network tab that its in the reqeust header) - and you're sure the url field is resolving correctly in your ajax request (`site_url(...)`)? Turn PHP error reporting on (right before sending the ajax request) to see if it's causing an error. https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – Kinglish Jun 10 '21 at 07:24
  • also what version of CI? – Kinglish Jun 10 '21 at 07:24
  • Yes im sure. The response code is 200 in network tabs – devax19 Jun 10 '21 at 08:42
  • version is 2.2.0 – devax19 Jun 10 '21 at 08:43
  • There is nothing in the console If i put console.log('something'); in the success area, i don't know why.. – devax19 Jun 10 '21 at 08:49