Javascript 从JQuery调用CodeIgniter方法

Javascript 从JQuery调用CodeIgniter方法,javascript,php,codeigniter,Javascript,Php,Codeigniter,我想使用jquery调用codeigniter方法。我的ajax调用正在工作,但出现错误。我已经添加了我的控制器、模型、ajax调用和错误 根据: $("body").on("click", ".call-ajax", function() { // obtém o valor do link console.log("chamada ajax"); var caminho = "http://localhost/xxxxx/public/uploads/anexos/

我想使用jquery调用codeigniter方法。我的ajax调用正在工作,但出现错误。我已经添加了我的控制器、模型、ajax调用和错误

根据:

$("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));
    });
  });
型号代码:

 public function delete_anexo($id, $file) {
        $this->db->delete($this->table, array('id_reclamacao' => $id, 'file' => $file));
        return true;
    }

您发布的第二个错误图像清楚地表明您的方法调用中缺少第二个参数,请在进行ajax调用时仔细检查这两个参数是否都已发布。

控制器
公共函数delete\u anexo($id,$file)中的此声明
假设
$id
$file
在url中,例如
reclamaco/delete\u anexo/{$id}/{$file}
,这显然不是您的
数据所需要的。因此,您需要像这样捕获post变量:

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

查看控制器如何具有两个参数:
公共函数delete\u anexo($id,$file)
您不传递任何参数
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);
}