Solved

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

Posted on 2016-11-04
2
41 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 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
Comment Utility
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
Comment Utility
@Chris

Perfectly as usual!

Thank you.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now