从JQuery调用CodeIgniter方法

I want to call codeigniter method using jquery. My ajax call is working but getting an error. I have added my controller, model, ajax call and error.

According to:

$("body").on("click", ".call-ajax", function() {
    // obtém o valor do link
    console.log("chamada ajax");

    var caminho = "http://localhost/xxxxx/public/uploads/anexos/";


    data = {
      id_rec: $(this).data("id_rec"),
      anexo: caminho + $(this).data("anexo")
    };

    console.log(data);

    // AJAX para o controller
    $.ajax({
      url: "reclamacao/delete_anexo",
      data: data,
      type: "POST"
    }).done(function(resp) {
        console.log("deleção OK");
      // Display the resposne
      //$("#result").append($("<li/>").html(resp));
    });
  });

It correctly calls

Check Image 1

But this error occurs:

Check image 2

My CONTROLLER CODE:

public function delete_anexo($id, $file)
{
    try
    {
        if (!$this->input->is_ajax_request())
        {
            $this->output->set_status_header(404);
            return;
        }
        if (!$this->anexo_model_reclamacao->delete_anexo($id, $file))
            throw new Exception("Erro ao excluir", 1);

        $alert = 'Operação Realizada com sucesso.';
    }
    catch (exception $e)
    {
        $alert = $e->getMessage();
    }

    bootbox_alert($alert);
}

MODEL CODE:

 public function delete_anexo($id, $file) {
        $this->db->delete($this->table, array('id_reclamacao' => $id, 'file' => $file));
        return true;
    }
dqvtm82066
dqvtm82066 看看你的控制器有两个参数:公共函数delete_anexo($id,$file)你没有通过任何参数
大约 2 年之前 回复

2个回答

This declaration in the controller public function delete_anexo($id, $file) assumes that the $id and $file are in the url e.g. reclamacao/delete_anexo/{$id}/{$file} which is clearly not what you want by your data jquery declaration. Thus you need to capture the post vars like so:

public function delete_anexo()
{
    try
    {
        if (!$this->input->is_ajax_request()) {
            $this->output->set_status_header(404);
            exit;
        }
        $id = $this->input->post('id_rec');
        $file = $this->input->post('anexo');
        if (is_null($id) || is_null($file)) {
            throw new Exception('Parameters missing');
        }
        if (!$this->anexo_model_reclamacao->delete_anexo($id, $file)) {
            throw new Exception("Erro ao excluir", 1);
        }
        $alert = 'Operação Realizada com sucesso.';
    }
    catch (exception $e)
    {
        $alert = $e->getMessage();
    }

    bootbox_alert($alert);
}



您发布的第二个错误图片清楚地表明您的方法调用中缺少第二个参数,请仔细检查是否两个 在进行ajax调用时,会发布参数。</ p>
</ div>

展开原文

原文

The second error image that you have posted is clearly stating that the second argument is missing from your method call, please double check whether both the arguments are getting posted when you are making the ajax call.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问