Solved

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

Posted on 2016-11-04
2
86 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

724 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