Example of processing ajax requests in PHP development framework kohana

Source: Internet
Author: User
Tags kohana
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.

      ,

      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.