RT. I checked a lot of information and found that the premise for differentiation is that the front end manually adds an x-requested-with field to the requestheader, but the native xhr does not include this header, therefore, if the front-end does not work with each other, the backend can directly determine that the data is a form... RT. I checked a lot of information and found that the premise for differentiation is that the front-end manually adds
x-requested-with
The field is included in the request header, but the native xhr does not include this header, so I would like to ask
If the front-end does not work togetherCan the background determine whether the data is submitted using form submit or ajax post?
Reply content:
RT. I checked a lot of information and found that the premise for differentiation is that the front-end manually addsx-requested-with
The field is included in the request header, but the native xhr does not include this header, so I would like to askIf the front-end does not work togetherCan the background determine whether the data is submitted using form submit or ajax post?
No. xhr also executes HTTP submission. By default, the headers are the same. However, if your front-end uses JQ, it will add the x-requested-with header.
Well, output this comparison $ _ 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's AJAX judgment is neither a standard header nor an industry standard.