【问题标题】:Codeigniter: AJAX request don't have response dataCodeigniter:AJAX 请求没有响应数据
【发布时间】:2021-06-09 19:02:58
【问题描述】:

我在 codeigniter 中有一个简单的应用程序。在我看来,我对控制器有 ajax POST 请求,但我没有收到响应数据。我在网络选项卡中的 chrome 开发工具中检查了响应,消息是:此请求没有可用的响应数据: 我的看法:

    <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>

我的控制器:

    <?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);
        }

}

在我的配置文件中,CSRF 保护被禁用!因为当它启用时,ajax 请求不起作用。提前感谢您的帮助!

【问题讨论】:

  • 所以您的问题实际上是关于如何在 CSRF 设置为 true 的情况下获取响应数据,例如 $config['csrf_protection'] = true; 请确认
  • 没有。启用 CSRF 保护后,ajax 请求不起作用。我收到一条消息:不允许
  • 我很困惑...

标签: ajax codeigniter request


【解决方案1】:

此代码存在一些问题。

#1:形成 ajax 请求的方式。你有type: 'POST' 的地方应该是method: 'POST' - 但不仅仅是一个错字,重要的是你要理解这些被称为HTTP 方法 - 就像你在@987654325 中指定的那样@ 标签。如果您不提供method,则假定为GET

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

另外,因为您指定了dataType:'json' - jquery 将提供一个准备使用的解析 JSON 对象。不用你自己解析

$.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 - 在您的 PHP 中,您可以使用 $_POST['key'] 捕获您的 key 变量(您在 ajax 中发送) - 尽管在 codeigniter 中您会得到它:

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

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

#3 - 使用 ajax,您实际上是 echo 响应,而不是 return 它:

public function deleteCai(){
  $status = array(
    "STATUS" => "true"
  );
  echo  json_encode($status);
}

【讨论】:

  • @devax19 - 这有帮助吗?
  • @devax19 感谢您的回复,但我仍然没有收到回复消息。
  • 并且您确定您的 POST 请求已被发送(您在网络选项卡中看到它在请求标头中)-并且您确定 url 字段在您的 ajax 请求中正确解析( site_url(...))?打开 PHP 错误报告(在发送 ajax 请求之前)以查看它是否导致错误。 stackoverflow.com/questions/1053424/…
  • 还有什么版本的CI?
  • 是的,我确定。网络标签中的响应代码为 200
猜你喜欢
  • 1970-01-01
  • 2017-06-29
  • 2016-03-30
  • 2022-12-07
  • 2015-11-16
  • 1970-01-01
  • 2019-08-30
  • 1970-01-01
  • 2023-02-20
相关资源
最近更新 更多