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