using jquery and kohana
well I havn't really ever using jquery to grab data from a server.. and well my jquery skills are pretty limited anyway so maybe im doing the jquery wrong ...im not sure
heres my controller to handle the json requests
class Controller_Json extends Controller_Template{
public function get_chat_entries(){
echo json_encode($this->db->get('chat_entries')->result());
}}
and heres my view that is meant to test it
<ul id="chats"&开发者_JAVA百科gt;
</ul>
<script>
$.getJSON('ajax/test.json', function(data) {
$('#chats').html('<li>' + data.text + '</li>');
});
</script>
I tried your code in my test base kohana. your js code is ok.
problem is mysql_resoure to json conversion.
i.e. "echo json_encode($this->db->get('chat_entries')->result());"
result method returns mysql_resoure which needs to be converted into array.
it works when if i put like this ...
echo json_encode($this->db->get('emails')->current());
... only one email is displayed inside of #chats ul list.
PS: emails is my table contains multiple email address
I can't really tell cause you don't provide any details about the error itself but I'd guess this might solve your problem:
http://de2.php.net/manual/en/function.json-encode.php#95667
If you are planning on using this function to serve a json file, it's important to note that the json generated by this function is not ready to be consumed by javascript until you wrap it in parens and add ";" to the end.
Cheers, aefxx
I think you are missing out on the document ready wrapper. I would wrap my jquery code inside a document.ready method like this :`
<script>
$(document).ready(function(){
$.getJSON('ajax/test.json', function(data) {
$('#chats').html('<li>' + data.text + '</li>');
});
});
</script>
I would also test the 'ajax/test.json' url from my browser to check if the server is returning a proper json string. Also make sure that the data object obtained inside your ajax callback function has a text property.
精彩评论