Home » JavaScript » X-Requested-With request header for Ajax

X-Requested-With request header for Ajax

The X-Requested-With request header is used at the server side to determine whether the request comes from the Ajax request or the traditional request.

two request is different from the requested Header, and the Ajax asynchronous request has one head parameter

more than the traditional synchronization request

1 traditional synchronous request parameter

accept, application/xml; q=0.9, q=0.8
* / *;
Accept-charset, GB2312, UTF-8; q=0.7, *; q=0.7

Accept-encoding, gzip, deflate

Accept-Language, zh-cn, Zh; q=0.5

Cache-control max-age=0

Connection keep-alive



Keep-alive 300

Referer XXX

User-agent Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv: Gecko/2009101601 Firefox/3.0.15 (.NET, CLR, 3.5.30729)

2 Ajax asynchronous request mode

accept */*

Accept-Language zh-cn

Referer xxx

x-requested-with XMLHttpRequest

, content-type, application/x-www-form-urlencoded, text/javascript

Accept-encoding, gzip, deflate

User-agent Mozilla/4.0 (compatible; MSIE 7; Windows NT 5.1; Trident/4.0; QQDownload 598;.NET; CLR; 2.0.50727;.NET; CLR; 3.0.04506.648;.NET


Content-Length 233

Connection Keep-Alive

Cache-control no-cache

Cookie CSS=undefined; JSESSIONID=1B9AC25036290F7FB6823CCE1A24E541

see the Ajax request with more than one x-requested-with. You can get request.getHeader ("X-Requested-With")

Bool isAjax = null;

String header = request.getHeader ("X-Requested-With");

If (null! = header & & header.equals ("XMLHttpRequest"))


IsAjax = true;


you can see that header is the traditional synchronous request for null. If XMLHttpRequest, Ajax requests.

Add: http://www.cnblogs.com/tony-jingzhou/archive/2012/07/30/2615612.html