RT。查了下很多资料里提到可以区分的前提都是前端手动添加一个x-requested-with
字段到request header里去,但是原生的xhr里不带这个头的,所以想问下如果前端不配合的情况下,后台可以直接判断出来数据是form表单的submit提交还是利用ajax的post提交的吗?
巴扎黑2017-04-10 16:17:25
public function isAjax()
{
if (!isset($_SERVER["HTTP_X_REQUESTED_WITH"])) {
return false;
}
$http_x_request = $_SERVER["HTTP_X_REQUESTED_WITH"];
if ( $http_x_request && strtoupper($http_x_request) == "XMLHTTPREQUEST") {
return true;
}
return false;
}
天蓬老师2017-04-10 16:17:25
/**
* Returns true if the request is a XMLHttpRequest.
*
* It works if your JavaScript library sets an X-Requested-With HTTP header.
* It is known to work with common JavaScript frameworks:
*
* @link http://en.wikipedia.org/wiki/List_of_Ajax_frameworks#JavaScript
*
* @return bool true if the request is an XMLHttpRequest, false otherwise
*
* @api
*/
public function isXmlHttpRequest()
{
return 'XMLHttpRequest' == $this->headers->get('X-Requested-With');
}
laravel中AJAX的判断,不是标准头也是业内标准了。