Home  >  Article  >  Backend Development  >  [php] php如何判断是不是ajax提交

[php] php如何判断是不是ajax提交

WBOY
WBOYOriginal
2016-06-23 14:29:44840browse

这里用到了jquery的ajax。

 

页面: 

        $.ajax({
            type: 'POST',
            url: 'http://www.yourwebsite.com',
            beforeSend:  function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("request_type","ajax");
            },
            data: 'test=test',
            success:  function(rs) {
                alert('ok');
                }
            }
        });

 

PHP代码:

echo  isset( $_SERVER['HTTP_REQUEST_TYPE']) &&  $_SERVER['HTTP_REQUEST_TYPE'] == "ajax" ? "it is ajax" : "it is NOT ajax.";

 

还有一种方法,js里面不用加beforeSend里面的代码,直接在PHP里面作判断:

echo  isset( $_SERVER['HTTP_X_REQUESTED_WITH']) &&  $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest' ? 'it is ajax' : 'it is NOT ajax';

 

PS:判断是否是Flash提交数据

echo  isset( $_SERVER['HTTP_USER_AGENT']) && ( stripos( $_SERVER['HTTP_USER_AGENT'],'Shockwave')!== false ||  stripos( $_SERVER['HTTP_USER_AGENT'],'Flash')!== false) ?  echo 'it is flash data' : 'it is NOT flash data';

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn