它检查请求是否由XMLHttpRequest发出,因为大多数浏览器在这种情况下发送带有此指示的标头:header HTTP_X_REQUESTED_WITH将被设置为XMLHttpRequest.
截至v2.3.4
/**
* Tests if the current request is an AJAX request by checking the
* X-Requested-With HTTP request header that most popular JS frameworks
* now set for AJAX calls.
*
* @return boolean
*/
public static function is_ajax()
{
return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
}
Run Code Online (Sandbox Code Playgroud)