Welcome Guest, Not a member yet? Register   Sign In
sending json using $this->output-set_output()
#1

[eluser]Pet[/eluser]
Hello there.I am actually running on this problem for days. I'm trying to send a json object to be handled by jquery $.get(). Here is the code for my controller.

$data = array(
// old
//'feeds' => $this->_feeds(0, $group, $friend_id, NULL, NULL, NULL, $header),
// new
'feeds' => $this->_feeds2($page = 0, $group, $friend_id, NULL, NULL, NULL, $header),
'owner_id' => $this->owner_id,
'friend_id' => $friend_id,
'group' => $group,
'type' => 'all',
'header' => $header,
);

$json = array(
'error' => FALSE,
'html' => $this->page->template($data, 'fragments/feeds'->(view template), NULL->(content_path_on template), TRUE),
);

$this->output
->set_content_type(HTTP_CONTENT_TYPE_JSON->"constant we use")
->set_output(json_encode($json));
}

This code is just a templating engine we use.
$this->page->template($data, 'fragments/feeds', NULL, TRUE)

Ok so I send an output. How can I specify the output to my jquery or how can I capture this output on my jquery?...any ideas?.. TNX
#2

[eluser]CroNiX[/eluser]
If I'm understanding you, you can't just send something "to" jQuery. jQuery can make a request and have the server respond with some result which you can receive in the callback event of the $.get() request.

Code:
$.get(
  'http://yoursite.com/some_controller/some_method',  //Url making request to
  function(data) {  //callback function
    console.log(data);  //data would be your json sent back from the controller
  }
);
#3

[eluser]InsiteFX[/eluser]
Code:
header('Content-type: application/json');
echo json_encode($response);

Plus what CroNiX stated above.
#4

[eluser]Pet[/eluser]
tnx guys.... im too lazy to see i have to make that jquery function to load something out of json request. Big tnx Cronix InsiteFX
#5

[eluser]CodeIgniteMe[/eluser]
I think this is enough on the back-end
(encodes PHP objects and arrays into JSON format)
Code:
echo json_encode($response);

and on the front-end/javascript
(ajax call for the URL and convert the response into JSON object. shorthand for $.ajax)
Code:
$.getJSON(URL, function(data){
alert(data);
});




Theme © iAndrew 2016 - Forum software by © MyBB