Solved

Could you point how to correctly shows a Codeigniter's method reply to a view?

Posted on 2016-11-08
9
14 Views
Last Modified: 2016-11-14
Hi Experts

Could you point how to correctly shows a Codeigniter's method reply to a view?

My try is (with no success)

jQuery code that call the Codeigniter's controller:
 $("body").on("click", ".call-ajax", function() {
          
   var id_rec = $(".id_reclamacao").val();
   var caminho = "<?php print base_url() . 'public/uploads/anexos/'; ?>";

   var postData = {
	 "id_rec": id_rec,
	 "anexo" : caminho + $(this).data("anexo")
	};

	$.ajax({
	  url: "reclamacao/delete_anexo",
	  data: postData,
	  type: "POST"
	}).done(function(resp) {
		
	  //Where I guess the reply is obtained
	  $("#result").append($("<li/>").html(resp));
	});
  });

Open in new window


Controller's code:
<?php
public function delete_anexo()
{
	try
	{
		if (!$this->input->is_ajax_request())
		{
			$this->output->set_status_header(404);
			return;
		}

		//EF 2016/Nov - 01 
		$id_reclamacao = $this->input->post('id_rec');
		$anexo = $this->input->post('anexo');


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

		$alert = 'Operação Realizada com sucesso.';
		
		//Where I tried to reply to the view:
		$this->output->set_content_type('application/json')->set_output(json_encode($alert));
	}
	catch (exception $e)
	{
		$alert = $e->getMessage();
	}

	bootbox_alert($alert);
}

Open in new window



View code:

<hr>
<div class="row">
	<div class="result">
	</div>
</div>

Open in new window


Thanks in advance!
0
Comment
Question by:Eduardo Fuerte
  • 5
  • 4
9 Comments
 
LVL 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 500 total points
ID: 41878613
It looks mostly fine apart from a few minor issues

$this->output->set_content_type('application/json')->set_output(json_encode($alert))

Open in new window

Suggests a JSON return but in your AJAX code you are not telling jQuery the return is AJAX - you should still get a response though.

$.ajax({
	  url: "reclamacao/delete_anexo",
	  data: postData,
	  type: "POST"
	}).done(function(resp) {
		
	  //Where I guess the reply is obtained
	  $("#result").append($("<li/>").html(resp));
	});

Open in new window

Should be
$.ajax({
	  url: "reclamacao/delete_anexo",
	  data: postData,
	  type: "POST",
          dataType: 'JSON'
	}).done(function(resp) {
		
	  //Where I guess the reply is obtained
	  $("#result").append($("<li/>").html(resp));
	});

Open in new window


What do you see in your Console (F12) when you do the AJAX call?
0
 

Author Comment

by:Eduardo Fuerte
ID: 41878662
Hi @Julian

The jQuery code accordingly you pointed:
 $.ajax({
	  url: "reclamacao/delete_anexo",
	  data: postData,
	  type: "POST",
	  dataType: "JSON"
	}).done(function(resp) {
		console.log("deleção OK");

	  $("#result").append($("<li/>").html(resp));
	});
  });

Open in new window


F12 - Console

img002
and

img003
0
 

Author Comment

by:Eduardo Fuerte
ID: 41878663
Still not running...
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41878790
As I noted in my earlier emails - the change recommended was minor in terms of the problem at hand. It was something to keep in mind but not a solution to the problem.

Can we see what the POST looks like expanded. Click the triangle to the left to expand the POST and paste screen shot of that.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Accepted Solution

by:
Eduardo Fuerte earned 0 total points
ID: 41880371
Hi @Julian

By folowing your suggestion (here and in other questions) I did:

$.ajax({
	url: "reclamacao/delete_anexo",
	data: postData,
	type: "POST"
}).done(function (resp) {
   $("#"+postData.id_anexo).parent().remove();
	//$("#result").append($("<li/>").html(resp));
});

Open in new window

0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41880458
Is that working for you? Seems counter intuitive. You replaced an append with a remove?
0
 

Author Comment

by:Eduardo Fuerte
ID: 41880810
Yes. Since when a file's deletion is successful the  link that represents this file is simply removed.
The message  "Successful deletion" isn't necessary - just the link is removed.
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41880835
Ah, understood - makes sense.
0
 

Author Closing Comment

by:Eduardo Fuerte
ID: 41886061
Thank you for the guidance!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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…
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 count occurrences of each item in an array.

762 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

23 Experts available now in Live!

Get 1:1 Help Now