Home >Backend Development >PHP Tutorial >Examples of processing ajax requests in the PHP development framework kohana, kohanaajax_PHP tutorial
What I’m sharing today is how to handle the ajax request of the page in kohana. It can be done in 2 steps. The premise is that your kohana framework can already run correctly, please pay attention.
1. The page makes a request.
The current mainstream javascript framework is jQuery. jQuery also encapsulates ajax requests. Here I will use jQuery as an example to write one. The demo is to obtain the background json string and use each to process it. Most of the code is from jqapi It’s accurate and convenient.
$.ajax({ url: "/test/json",//test是控制器,json是action,带/是相对站点根目录的意思 dataType:json, // data: 这里的写法一般是拼字符串,'id=1&name=jack'这种. 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'); } });
2. Processing in kohana, return json string. Above code
public function action_json() { $this -> auto_render = FALSE;//不需要view if ($this -> request -> is_ajax()) //判断是否为ajax请求 { //get $arr here. echo json_encode($arr);//建议这样写,避免0或其他情况. exit; } // json 只支持 utf-8 编码,这点很重要,切记啊!!! }
ok, I believe that after reading these two pieces of code, you will definitely understand how to handle ajax requests in kohana.
PS: The front-end js must be encoded in utf-8, please pay attention, dear.
not found MODPATH\\database\\classes\\kohana\\db.php [ 63 ] 58 * @param Please indicate how you call database or ORM in the process. It may be that the method you call is wrong. .
The PHP framework is just a relatively standardized class. For ajax and permission management, you have to write your own classes.
I personally recommend thinkphp because there are many tutorials on the Internet. I am learning this framework.