Php determines whether it is an ajax request method, and php judges an ajax request

Source: Internet
Author: User

Php determines whether it is an ajax request method, and php judges an ajax request

The example in this article describes how to determine whether php is an ajax request. We will share this with you for your reference. The details are as follows:

First, how to differentiate the front-end when using jQuery:

When jQuery sends an ajax request, a message named X-Requested-With is added to the request header. The message content is XMLHttpRequest.

You can use $ _ SERVER ["HTTP_X_REQUESTED_WITH"] On the backend to obtain the data. (Note: The dashes are replaced by underscores, which are case-insensitive)

Therefore, we can determine whether the request is an ajax request:

If (isset ($ _ SERVER ["HTTP_X_REQUESTED_WITH"]) & strtolower ($ _ SERVER ["HTTP_X_REQUESTED_WITH"]) = "xmlhttprequest ") {// ajax request processing method} else {// normal request processing method };

When using native JavaScript to send an ajax request, we can also add information to the header to facilitate the differentiation of backend students. The method is as follows:

Var xmlhttp = new XMLHttpRequest (); xmlhttp. open ("GET", "test. php ", true); xmlhttp. setRequestHeader ("X-Requested-With", "XMLHttpRequest"); xmlhttp. send ();

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.