PHP detects Ajax call requests

Source: Internet
Author: User

For example, to report an error to the user, use the following method:
You can use set_error_handler to set your own error handling function. trigger_error is used in the error reporting area, and an error information page is displayed in the custom error handling function.

However
If the page that calls trigger_error is triggered by an Ajax request, a lot of HTML code is returned by ajax to display the error. While average
Ajax calls all agree on their own error information format, so the backend PHP code either uses two different reporting methods: one for Ajax call requests, refresh a common page
Use, or use a unified error report format. When reporting, determine whether this is an Ajax call request or a normal page refresh request.

In PHP code, the method for detecting Ajax call requests is as follows:
If (isset ($ _ server ['HTTP _ x_requested_with ']) &
Strtolower ($ _ server ['HTTP _ x_requested_with ']) = 'xmlhttprequest '){
// This is an Ajax request
}
This method is from Google search and is said to be only applicable to Ajax requests sent by jquery.

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.