【问题标题】:Can't post value to codeigniter controller with ajax无法使用 ajax 将值发布到 codeigniter 控制器
【发布时间】:2019-10-16 08:06:12
【问题描述】:

所以我想通过使用 ajax 来做一个更新功能。我有两个参数,我想将参数传递给我的 CI 控制器。但我得到了一个错误,我认为这是没有传递给控制器​​的参数。

这是我的 ajax 函数:

function suspend(tipe, id){
    var postData = {
        'tipe' : tipe,
        'id' : id
    };
    $.ajax({
        type: "POST",
        url: "<?=site_url("amas/c_agency/suspend_agency/");?>",
        data: postData,
        success: function(data){
            if(tipe == 0){
                $("#suspend").attr("onclick","suspend(1,"+id+")");
            }else{
                $("#suspend").attr("onclick","suspend(0,"+id+")");
            }
            alertify.success("Agency berhasil disuspend!");
        } ,
        error: function() {
            alertify.error("Gagal suspend agency!");
        }
    });
}

这是我的 codeigniter 控制器:

function suspend_agency(){
        $session['hasil'] = $this->session->userdata('logged_in');
        $login_id = $session['hasil']->login_id;

        $tipe = $this->input->post('tipe');
        $id = $this->input->post('id');

        if($tipe == 0){
            $id = array(
                'id_agency' => $id
            );
            $data = array(
                'is_suspend' => '1',
                'suspend_by' => $login_id,
                'suspend_at' => date("Y-m-d")
            );
        }else{
            $id = array(
                'id_agency' => $id
            );
            $data = array(
                'is_suspend' => '0'
            );
        }

        $result = $this->m_agency->suspend_agency($id, $data);
    }

我尝试将 ajax 数据更改为数据:"tipe="+tipe+"&amp;id="+id 但它也不起作用。

【问题讨论】:

  • 请检查您的控制台是否在您的 AJAX 请求中传递了这些值。
  • 是的,该值正在传递到我的 ajax 请求中,但我认为该值没有传递到控制器中
  • 在每个 AJAX 调用的网络选项卡中都有一个响应选项卡。
  • 尝试将site_url 更改为base_url。希望它对你有用。
  • "但我遇到了错误",请说明是哪个错误。还要检查您的网址是否正确,您在另一个问题中使用ama/c_agency/function 而不是amas/...

标签: php jquery ajax codeigniter codeigniter-3


【解决方案1】:

试试这个来打印你的变量。 echo json_encode($result);

function suspend_agency(){
    $session['hasil'] = $this->session->userdata('logged_in');
    $login_id = $session['hasil']->login_id;

    $tipe = $this->input->post('tipe');
    $id = $this->input->post('id');

    if($tipe == 0){
        $id = array(
            'id_agency' => $id
        );
        $data = array(
            'is_suspend' => '1',
            'suspend_by' => $login_id,
            'suspend_at' => date("Y-m-d")
        );
    }else{
        $id = array(
            'id_agency' => $id
        );
        $data = array(
            'is_suspend' => '0'
        );
    }

    $result = $this->m_agency->suspend_agency($id, $data);
    echo json_encode($result);
}

【讨论】:

  • OP 说他们收到了一个错误(还不确定是什么),但添加 echo 不太可能改变这一点
  • 问题是没有从控制器返回值,而是将值获取到控制器
  • 打印变量将显示您必须纠正的错误以及您的 ajax 等待响应数据
  • 我还复制了你的代码并做了测试,在控制器中你得到了 post 发送的数据
  • 并删除 amas/ &lt;? = site_url ("amas / c_agency / suspend_agency /");?&gt; 并把这个 &lt;? = site_url ("c_agency / suspend_agency /");?&gt; 因为我想 c_agency 是你的控制器和 suspend_agency 是你的方法
猜你喜欢
  • 2011-07-28
  • 1970-01-01
  • 1970-01-01
  • 2021-09-07
  • 2021-04-30
  • 1970-01-01
  • 2012-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多