douhui5953 2018-08-01 05:53
浏览 75
已采纳

从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;
    }
  • 写回答

2条回答 默认 最新

  • dongyao2022 2018-08-01 06:02
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退