search
Homephp教程php手册PHP 判断是否为 AJAX 请求

先说前端使用 jQuery 时怎么区分:

  jQuery 发出 ajax 请求时,会在请求头部添加一个名为 X-Requested-With 的信息,信息内容为:XMLHttpRequest

  在后端可以使用 $_SERVER["HTTP_X_REQUESTED_WITH"] 来获取。(注意:中划线换成了下划线,不区分大小写)

  由此,我们可以这样来判断是否为 ajax 请求:

<span>//</span><span> php 判断是否为 ajax 请求  http://www.cnblogs.com/sosoft/</span>
<span>if</span>(<span>isset</span>(<span>$_SERVER</span>["HTTP_X_REQUESTED_WITH"]) && <span>strtolower</span>(<span>$_SERVER</span>["HTTP_X_REQUESTED_WITH"])=="xmlhttprequest"<span>){ 
    </span><span>//</span><span> ajax 请求的处理方式 </span>
}<span>else</span><span>{ 
    </span><span>//</span><span> 正常请求的处理方式 </span>
};

 

在使用原生 JavaScript 发出 ajax 请求时,我们也可以给头部添加信息,以方便后端同学进行区分,方法如下:

<span>1</span> <span>var</span> xmlhttp=<span>new</span><span> XMLHttpRequest(); 
</span><span>2</span> xmlhttp.open("GET","test.php",<span>true</span><span>); 
</span><span>3</span> xmlhttp.setRequestHeader("X-Requested-With","XMLHttpRequest"<span>); 
</span><span>4</span> xmlhttp.send();

 

这里我们也一样是给头部添加 X_REQUESTED_WITH 信息,与 jQuery 的一致。当然你也可以更改为别的信息来进行区分。

 

  OK,进行区分之后有什么好处呢?说两个例子:

  1.当 js 文件未加载完时,用户点击了某个按钮或链接,本应是 ajax 请求的成了 正常请求,后端根据判断,不输出 ajax 时的 json 数据,而是跳转,这也是优雅降级的形式。

  2. [A 页面]使用 ajax 方式进行登录,[B 页面]使用正常方式登录,如果不区分,后端需要写两次几乎完全相同的代码,而有了区分,可以把重复的代码消掉。

开启PHP的伪静态 http://www.cnblogs.com/sosoft/p/3549336.html

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor