Today we share the AJAX request to process the page in Kohana. 2 steps. The premise is that your Kohana framework has been able to run correctly, notice.
1. The page makes a request.
Now the mainstream JavaScript framework is not jquery. jquery encapsulates Ajax requests, and here's how jquery is written. The demo is to get the back-end JSON string and use each to process it. The code is mostly from the JQAPI, accurate and convenient.
$.ajax ({
URL: "/test/json",//test is controller, JSON is action, with/is relative to the site root directory
Datatype:json,
//data: This is usually a spelling string, ' Id=1&name=jack ' this.
Success:function (data) {
var items = [];
$.each (data, function (key, Val) {
items.push (' <li id= ' + key + ' > ' + val + ' </li> ');
});
$ (' <ul/> ', {
' class ': ' My-new-list ',
html:items.join (')
}). Appendto (' body ');
}
);
Processing in 2.kohana, returns the JSON string. Code on
Public Function Action_json ()
{
$this-> Auto_render = false;//does not need view
if ($this-> request-> Is_aj AX ())//Determine if Ajax request
{
//get $arr here.
echo Json_encode ($arr);//recommended to avoid 0 or other cases.
Exit;
}
JSON only supports UTF-8 encoding, this is important, remember!!!
}
OK, I believe that after reading these 2 pieces of code, Kohana in the processing of AJAX requests, you must understand.
PS: The front desk js must be Utf-8 code, pay attention to Oh, pro.