Problem with JQuery getJSON retrieve data from Controller (Code igniter)?

This is how I handle my jQuery Codeigniter calls and it works for me.

Up vote 0 down vote favorite 2 share g+ share fb share tw.

I have a problem with my JQuery, Ajax, and PHP code. I have a page containing all the post from a user. Then in this page, I want to load the latest comment for that post by using Ajax.

However it doesn't show up anything. Here is the PHP code where I called the JQuery function: Latest Comment: And this is my PHP Code Igniter Controller that handle the request: class postC extends CI_Controller { function __construct() { parent::__construct(); $this->load->model('models_facade'); $this->load->Database(); $this->load->helper('url'); $this->load->helper('form'); $this->load->library('tank_auth'); $this->load->library('user_session_lib'); error_reporting(E_ALL ^ (E_NOTICE | E_WARNING)); } function latest_comment(){ $post_id = $this->checkValues($_GET'post_id'); $latestcomment = $this->models_facade->getLatestCommentForPost($post_id); return json_encode($latestcomment); } } I have tested to call the function manually and display the latest comment without using ajax, and it worked. So this means that there is nothing wrong with the back end function in the modelthat retrieve the result from mysql.

I have also tried to do : echo json_encode($latestcomment); when trying to call the function manually, and I could see that the data was in JSON format. So this means, that there is something wrong with the ajax call, that it did not get the data from the controller. I used GET for .

GetJSON, and POST for .post() and .ajax(). I have tried all versions of the jQuery Ajax call: .getJSON(), .post(), and .ajax() , and none of it worked. I am sure that the corresponding Jquery libraries have been loaded properly, since all my other ajax functions could work properly.

Does anyone has any clue where the errors could be? I have been debugging this for one full day, and got no result. Thanks php json codeigniter jquery-ajax link|improve this question asked Mar 24 '11 at 11:08all_by_grace751220 81% accept rate.

Use a debugger to detect whether I successfully executes the AJAX call and what the reply is. – ZeissS Mar 24 '11 at 14:03.

This is how I handle my jQuery Codeigniter calls and it works for me. Try this for jQuery: $(function(){ $. Post("postC/latest_comment", {post_id: post_id}, function(data){ $("#result").

Prepend(data. Html); },'json'); }); PHP: function latest_comment(){ $post_id = $this->checkValues($_GET'post_id'); $jsonData'html' = $this->models_facade->getLatestCommentForPost($post_id); echo json_encode($jsonData); } This will allow you to put more in the JSON object that is returned. One trick I like to use is adding a "failed" and "msg" value to the array.

So if the user needs to be notified then I can tell the user it failed and maybe why. Hope that helps // lance.

My guess is you have not enabled query string in your config file, may be this will work for you, javascript: $(function(){ $. GetJSON("postC/latest_comment/" + post_id, function(res){ $("#result"). Prepend(res.

Text); }); }); and in php controller: function latest_comment($post_id) { $post_id = $this->checkValues($post_id); $latestcomment = $this->models_facade->getLatestCommentForPost($post_id); // Better practise to put json header header('Cache-Control: no-cache, must-revalidate'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Content-type: application/json'); return json_encode($latestcomment); }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions