How does Kohana determine if a request is an AJAX one?
Kohana如何确定请求是否是AJAX?
Is there anything different in the referrer string? Do I need to add a GET param, perhaps ?ajax=true
?
引用字符串中有什么不同吗?我是否需要添加GET参数?ajax = true?
3 个解决方案
#1
8
It checks if the request is made by XMLHttpRequest since most browser send a header in this case with this indication: header HTTP_X_REQUESTED_WITH
would be set to XMLHttpRequest
.
它检查请求是否由XMLHttpRequest发出,因为大多数浏览器在这种情况下使用此指示发送标头:header HTTP_X_REQUESTED_WITH将设置为XMLHttpRequest。
#2
5
As of v2.3.4
截至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');
}
#3
1
Necro-posting because this came up first my google search.
Necro发布,因为这首先是我的谷歌搜索。
Dunno if Kohana still works this way, but using HTTP_X_REQUESTED_WITH is no longer best practice.
Dunno如果Kohana仍然以这种方式工作,但使用HTTP_X_REQUESTED_WITH不再是最佳实践。
Ajax requests -- all HTTP requests actually -- should send an "Accept" header.
Ajax请求 - 实际上所有HTTP请求 - 应该发送“Accept”标头。
Any server-side process should examine the "Accept" header to determine what content to send in response. One way to do this in PHP is:
任何服务器端进程都应检查“Accept”标头,以确定要响应的内容。在PHP中执行此操作的一种方法是:
function is_ajax() {
return $_SERVER['HTTP_ACCEPT'] == 'application/json';
}
#1
8
It checks if the request is made by XMLHttpRequest since most browser send a header in this case with this indication: header HTTP_X_REQUESTED_WITH
would be set to XMLHttpRequest
.
它检查请求是否由XMLHttpRequest发出,因为大多数浏览器在这种情况下使用此指示发送标头:header HTTP_X_REQUESTED_WITH将设置为XMLHttpRequest。
#2
5
As of v2.3.4
截至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');
}
#3
1
Necro-posting because this came up first my google search.
Necro发布,因为这首先是我的谷歌搜索。
Dunno if Kohana still works this way, but using HTTP_X_REQUESTED_WITH is no longer best practice.
Dunno如果Kohana仍然以这种方式工作,但使用HTTP_X_REQUESTED_WITH不再是最佳实践。
Ajax requests -- all HTTP requests actually -- should send an "Accept" header.
Ajax请求 - 实际上所有HTTP请求 - 应该发送“Accept”标头。
Any server-side process should examine the "Accept" header to determine what content to send in response. One way to do this in PHP is:
任何服务器端进程都应检查“Accept”标头,以确定要响应的内容。在PHP中执行此操作的一种方法是:
function is_ajax() {
return $_SERVER['HTTP_ACCEPT'] == 'application/json';
}