将信息从 AJAX 传递到控制器 class

Passing information from AJAX to controller class

我目前正在我的 URL 中传递值,我想获取这些值并将其插入我的控制器 class 以进行过滤。我当前的 URL 看起来像这样:http://localhost/reports/lists?source=Product1&status=4。现在要获取假设源的值,我使用以下代码:

  let searchParams = new URLSearchParams(window.location.search);
  var status = searchParams.get('source');

现在我希望这个状态变量转到我的控制器 class 以便我可以将它用作我的模型的参数之一 class:

完整代码: 查看 Class:

<?php  
$postd = json_encode(array_filter($post));
?>
<table id="item-list">
            <tr> 
                <th>Ref.No#</th>
                <th>Source</th>
            </tr>
</table>

<script>
$(document).ready(function() {
  function sendreq(){
    setpostdatas();cleartable();getleads();
  }

   var userrole = "<?php echo $this->session->userdata('clientrole')?>";
   var slug = '<?php echo $slug?>';
   var postd = '<?php echo $postd; ?>';
if( userrole > 1 && userrole != 5){
    $('#item-list').DataTable({
        "processing": true,
        "stateSave": true,
        "serverSide": true,
        "ordering": false,
        "ajax": {
            url: "<?php echo site_url(); ?>reports/loadLeads",
            data: {slug: slug, postdata: postd, status: status},
            type : 'POST',
            "dataSrc": function ( d ) {
                d.myKey = "myValue";
                if(d.recordsTotal == 0 || d.data == null){
                   $("#item-list_info").text("No records found");
                    $("#item-list_processing").css("display","none");
                }
                return d.data;
            }
        },
        'columns': [
            {"data": "id", "id": "id"},
            {"data": "refno", "refno": "refno"},
            {"data": "source", "source": "source"},   
        ]
    });
  }

控制器Class:

public function loadLeads($p=''){
        $leadsource = $this->input->get('status');
        if(isset($_POST['postdata'])){
            if($_POST['postdata'] != null && $_POST['postdata'] != 'null'){
                $post=$_POST['postdata'];
            }
            $post = json_decode($post,true);
            unset($post['slug']);
            unset($post['page']);
            $sort = $post['afsort'];
            if($sort == "asc"){
                $sortQ = 'l.updated_date asc,';
            }else if ($sort == "desc"){
                $sortQ = 'l.updated_date desc,';
            }
        }
        $offset = (int)$_POST['start'] ;
        $pstdatas = explode(',', $_POST['postdata']);
        unset($pstdatas['item-list_length']);
        if($this->session->userdata('clientrole') == 1 || $this->session->userdata('clientrole') == 5 ){
            $content['leads']=$this->leads_model->get_pagination($_POST['length'],$offset,$where,'',false,$sortQ?$sortQ:'l.assign_status =\'Unassigned\' desc,',$all,$leadsource);          
        }else{
            $content['leads']=$this->leads_model->get_pagination($_POST['length'],$offset,$where,'',false,$sortQ?$sortQ:'l.assigned_date desc,',$all,$leadsource);
        }

现在在我的控制器中 class 我希望传递 AJAX 变量,以便我可以在模型查询中使用它。

编辑: 我现在发现问题出在我的类型变量上。截至目前,我在 AJAX 视图中使用 type : 'POST'。现在,如果我将其更改为 get,我可以看到 searchParams.get('source') 输出,但其他数据会出错,因为那些需要 POST。那么现在如何在我的 ajax 代码中使用两种类型的 GET 和 POST。

  1. 你不使用 $p
  2. 你没有传$_GET - "<?php echo site_url(); ?>reports/loadLeads",一定是"<?php echo site_url(); ?>reports/loadLeads?status=xxx"
  3. 您必须使用 $this->input->post('...') 而不是直接访问