Solved

Could you point how to correctly call a Codeigniter method from jQuery?

Posted on 2016-11-04
2
67 Views
Last Modified: 2016-11-04
Could you point how to correctly call a Codeigniter method from jQuery?

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

Open in new window


It correctly calls
img002

But this error occurs:
img001
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);
}

Open in new window



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

Open in new window



Thanks in advance!
0
Comment
Question by:Eduardo Fuerte
2 Comments
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 41874241
In your controller code, you're expecting the variables $id and $file to be passed as part of the URL So this:

public function delete_anexo($id, $file)

is expecting you to call it like so:

delete_anexo/1/file

In your jQuery code, you're POSTing the data to delete_anexo, so you need to get the variables from the post array

public function delete_anexo()
{
	try
	{
		if (!$this->input->is_ajax_request())
		{
			$this->output->set_status_header(404);
			return;
		}

		$id = $this->input->post('id_rec');
		$file = $this->input->post('anexo');

		if (!$this->anexo_model_reclamacao->delete_anexo($id, $file))
			throw new Exception("Erro ao excluir", 1);

		...

Open in new window

1
 

Author Closing Comment

by:Eduardo Fuerte
ID: 41874413
@Chris

Perfectly as usual!

Thank you.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question