This article describes how to process ajax requests in the PHP development framework kohana. kohana is a PHP5 development framework. For more information about Ajax, see
Today, we will share with you how to process ajax requests on the page in kohana. Step 2: Make sure that your kohana framework is running correctly.
1. the page sends a request.
Currently, mainstream javascript frameworks are not jQuery. jQuery also encapsulates ajax requests. here we use jQuery as an example to write. demo is to obtain the background json string and process it with each. most of the code is developed from jqapi, which is accurate and convenient.
$. Ajax ({url: "/test/json", // test is the controller, json is the action, and/is the meaning of dataType: json, // data: here, we usually write strings, such as 'Id = 1 & name = Jack. success: function (data) {var items = []; $. each (data, function (key, val) {items. push ('
'+ Val +'');}); $ ('
', {'Class': 'My-new-list', html: items. join ('')}). appendTo ('body ');}});
2. processing in kohana, return the json string. on the code
Public function action_json () {$ this-> auto_render = FALSE; // view if ($ this-> request-> is_ajax () is not required ()) // Determine whether the request is an ajax request {// get $ arr here. echo json_encode ($ arr); // We recommend that you write this code to avoid zero or other conditions. exit;} // json only supports UTF-8 encoding. this is very important. remember !!! }
OK. I believe you will understand how to process ajax requests in kohana after reading these two codes.
PS: The front-end JavaScript code must be UTF-8 encoded. pay attention to this.