This article describes how to process ajax requests in the PHP development framework kohana. kohana is a PHP5 development framework. For more information, see
This article describes how to process ajax requests in the PHP development framework kohana. kohana is a PHP5 development framework. For more information, 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; // viewif ($ 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.
,