[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2016-11-08
9
Medium Priority
?
35 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
[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
  • 5
  • 4
9 Comments
 
LVL 59

Assisted Solution

by:Julian Hansen
Julian Hansen earned 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 59

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 59

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 59

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

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.
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.
The viewer will learn how to dynamically set the form action using jQuery.
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…
Suggested Courses

656 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