Example of processing AJAX requests in PHP development Framework Kohana _php Example

Source: Internet
Author: User
Tags kohana

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.