How Codeigniter handles common requests and ajax requests

Source: Internet
Author: User
In Codeigniter, all requests generally correspond to a function in the Controller. For different requests, such as ajax and common requests, the returned items are generally different, such as this-& gt; load-& gt; view or direct echo. Writing these two methods together makes me feel particularly uncomfortable... in Codeigniter, all requests generally correspond to a function in the Controller. If different requests, such as ajax and common requests, the returned items are generally different, such as this-> load-> view or direct echo.

Writing the two methods together makes me feel particularly uncomfortable. I imagine a solution that is to use the Accept in the Http header. How to generate a method is in a function, different requests output different view styles. For example, if ajax requests this function, the system returns the json format and HTML for normal requests. How can I achieve this.

Reply content:

In Codeigniter, all requests generally correspond to a function in the Controller. If different requests, such as ajax and common requests, the returned items are generally different, such as this-> load-> view or direct echo.

Writing the two methods together makes me feel particularly uncomfortable. I imagine a solution that is to use the Accept in the Http header. How to generate a method is in a function, different requests output different view styles. For example, if ajax requests this function, the system returns the json format and HTML for normal requests. How can I achieve this.

When an ajax request is sent to a disk, only json data is returned, and HTML is not directly returned. The view is directly include view. The principle is different. If you need such a view, then you can customize the view method:

Public function loadView ($ view, $ data) {if ($ this-> input-> is_ajax_request () {echo $ this-> load-> view ($ view, $ data, TRUE); exit;} // If ajax is used, it will not be executed here $ this-> load-
  

When you load a view, use $ this-> loadView ($ viewName, $ data) instead of $ this-> load-> view, you can write this loadView method in the parent Contrller. This is just an idea. You can customize it as needed!

The is_ajax_request () method has been provided in the input class of CodeIgniter to determine whether it is an ajax request. It can be called in the form of $ this-> input-> is_ajax_request.

See http://codeigniter.org.cn/user_guide/libraries/input.html.

First, we do not recommend that you design a function like this based on the "single function principle. You can encapsulate the main functions into a callable method, and then call this method in different controllers for processing. In addition, json is also a form of view display, so you can upload all the data into the view, but the difference between two views is that one is HTML display and the other is json display.

Why not add a request parameter to the request to differentiate terminals?

You can tell the difference between UA and UA.

Define ('isAJAX ', isset ($SERVER ['HTTPXREQUESTEDWITH ']) & strtolower ($SERVER ['HTTPXREQUESTED_WITH ']) = 'xmlhttprequest ');

In this case, if (IS_AJAX ){...} Else {...}

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.