Can ajax-php directly differentiate form submission and xhr post submission?

Source: Internet
Author: User
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-withThe 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-withThe 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.