Ajax-php can directly distinguish between form form submission and XHR post submission?

Source: Internet
Author: User
Rt. We've got a lot of information. The premise that can be differentiated is that the front-end manually adds a x-requested-withfield 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.

  • 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.