Php verification request page Source: if ($ _ server [#39; http_x_requested_with #39;] #39; xmlhttprequest #... php verification request page Source:
if( $_server['http_x_requested_with'] == 'xmlhttprequest' ) { echo 'ajax'; } else { echo 'normal'; }
When jquery implements ajax internally, it has already added the identifier, which is shown in the jquery source code:
xhr.setrequestheader("x-requested-with", "xmlhttprequest");
Therefore, in php, you can use http_x_requested_with to determine whether another implementation is required. The following describes the principle: when sending an ajax request, we can use the xmlhttprequest object, create a custom header. if you are using the native ajax method, that is, the ajax method that is not packaged using jquery or other js frameworks, the code is as follows:
Xmlhttprequest. setrequestheader ("request_type", "ajax ");
With jquery's $. ajax () method, you can easily create custom header information before sending an ajax request.
$.ajax({ type: "get", url: base_url + 'php_check_ajax_request/get_user_list.html', beforesend: function (xmlhttprequest) { xmlhttprequest.setrequestheader("request_type","ajax"); }, success: function(data){ $("#user_list").html(data); $tip.hide(); $button.attr('disabled',true); } });
When retrieving data in php:
If (isset ($ _ server ['http _ request_type ']) & $ _ server ['http _ request_type'] = "ajax ") {// ajax submit} else {// non-ajax submit}
Link to this article:
Add to favorites ^ please keep the tutorial address.