Solved

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

Posted on 2016-11-08
9
26 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 55

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 55

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
 

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 55

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 55

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

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 …
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 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