This article illustrates how CI detection is a way to submit data in Ajax or page post. Share to everyone for your reference. The implementation methods are as follows:
First, the question:
Because the project requires that we want to know whether the data source is submitted by Ajax or the data submitted by the post of the page to be processed at different levels.
Second, the solution:
The workaround in PHP is as follows:
If it is an AJAX request, the value of the following expression is true
Copy Code code as follows:
$_server["Http_x_requested_with"]== "XMLHttpRequest"
is a PHP environment variable.
CI in the treatment method:
Copy Code code as follows:
Define (' Is_ajax ', isset ($_server[' Http_x_requested_with ']) && strtolower ($_server[' Http_x_requested_with ') ] = = ' XMLHttpRequest ');
Define ("Is_post", Strtolower ($_server[' request_method ') = = ' POST ');
Remember in the use of thinkphp has built-in two constants Is_ajax and Is_post, want to use in CI for half a day seems to have not found, then automatically hands-clothed
Add the top two lines of code to the config/constants.php configuration file of your project, and you can call it directly in all methods
For example:
Copy Code code as follows:
if (is_post) {
...
}
if (Is_ajax) {
...
}
I hope this article will help you with the CI Framework program design.