On a server it can be useful to know that an incoming request is AJAX.
Most js libraries use XMLHttpRequest
and so provide HTTP_X_REQUESTED_WITH: XMLHttpRequest
, but neither Chrome's implementation nor Github's polyfill of the new fetch
uses a similar header. So how can one detect that the request is AJAX?
Why is a request identifying its initiator not enforced through standards for fetch
and XMLHttpRequest
? Should something else be used for decision-making (e.g. clients providing the content-type they expect in response)?
Check out this issue on the Github's polyfill repository, specially this comment.
Since the X-Requested-With
header is not an standard, they are using a wrapper that provides some of the missing behavior.
If you need more guidance, check this lines of the wrapper code:
function headers(options) {
options = options || {}
options.headers = options.headers || {}
options.headers['X-Requested-With'] = 'XMLHttpRequest'
return options
}