代码之家  ›  专栏  ›  技术社区  ›  Shalin Nipuna

从JQuery调用CodeIgniter方法

  •  -1
  • Shalin Nipuna  · 技术社区  · 8 年前

    我想使用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));
        });
      });
    

    它正确地调用

    Check Image 1

    但是这个错误发生了:

    Check image 2

    我的 控制器代码 :

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

    型号代码:

     public function delete_anexo($id, $file) {
            $this->db->delete($this->table, array('id_reclamacao' => $id, 'file' => $file));
            return true;
        }
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   Alex    8 年前

    控制器中的声明 public function delete_anexo($id, $file) 假设 $id $file 在url中,例如。 reclamacao/delete_anexo/{$id}/{$file} 这显然不是你想要的 data jquery声明。因此,您需要像这样捕获post vars:

    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);
    }
    
        2
  •  0
  •   Ankur Tailang    8 年前

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

    推荐文章