Home  >  Article  >  Backend Development  >  Examples of processing ajax requests in the PHP development framework kohana, kohanaajax_PHP tutorial

Examples of processing ajax requests in the PHP development framework kohana, kohanaajax_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:23:19766browse

An example of processing ajax requests in the PHP development framework kohana, kohanaajax

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.

How to configure the php kohana framework after downloading it?

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. .

Which domestic PHP rapid development framework is better?

that supports ajax and permission management

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/840642.htmlTechArticleAn example of processing ajax requests in the PHP development framework kohana, kohanaajax What I’m sharing today is processing ajax requests for pages in kohana .2 steps to get it done. The premise is that your kohana framework can already run correctly...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn