Solved

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

Posted on 2016-11-08
9
19 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 52

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 52

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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 52

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 52

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

895 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

12 Experts available now in Live!

Get 1:1 Help Now