Rt. We've got a lot of information. The premise that can be differentiated is that the front-end manually adds a
x-requested-with
field to the request header, but the native XHR does not carry this head, so I want to ask
If the front end is not mated, the background can be directly judged whether the data is the form form of submit submission or the use of Ajax post submission?
Reply content:
Rt. Check out a lot of information mentioned can be differentiated premise is the front-end manually add a x-requested-with
field to the request header, but the original XHR without this head, so want to ask if the front end does not match the case , The background can be directly judged whether the data is the form form of submit submission or the use of Ajax post submission?
No, XHR also executes HTTP commits, and the default header is the same. But if you use JQ at the front, it will join X-requested-with's head.
Well output This comparison under $_server["Http_x_requested_with"]
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;}
/** * 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 in the Ajax judgment, not the standard head is also the industry standard.