Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2016-11-04
2
Medium Priority
?
99 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 44

Accepted Solution

by:
Chris Stanyon earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

618 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