Fetch: X-Requested-With request header is not set with XMLHttpRequest

Created on 19 Oct 2014  路  4Comments  路  Source: github/fetch

Some back-ends check on X-Requested-With header to see if it is an ajax request. With fetch, that header isn't set.

Most helpful comment

The XMLHttpRequest used by the fetch polyfill is an implementation detail. When browsers start shipping window.fetch natively, the X-Requested-With header won't be sent along with the request. So it would be an error to rely on that header being sent from the polyfill.

Here's a simple fetch wrapper we're using at GitHub that provides some of the missing behavior from Rails' integration with jQuery.

https://gist.github.com/dgraham/92e4c45da3707a3fe789

In practice, sites are going to need a small wrapper like this to work with window.fetch as easily as jQuery's $.ajax.

/cc @annevk This wrapper might or might not inform the fetch spec.

All 4 comments

X-Requested-With is just a header convention, its not part of any XHR or the Fetch spec.

The XMLHttpRequest used by the fetch polyfill is an implementation detail. When browsers start shipping window.fetch natively, the X-Requested-With header won't be sent along with the request. So it would be an error to rely on that header being sent from the polyfill.

Here's a simple fetch wrapper we're using at GitHub that provides some of the missing behavior from Rails' integration with jQuery.

https://gist.github.com/dgraham/92e4c45da3707a3fe789

In practice, sites are going to need a small wrapper like this to work with window.fetch as easily as jQuery's $.ajax.

/cc @annevk This wrapper might or might not inform the fetch spec.

:+1: Thanks for the information!

@dgraham that wrapper seems to assume a document environment. The CSRF stuff would break in a worker environment.

@dgraham if you want standardized CSRF tokens, perhaps email [email protected] or [email protected] (need to subscribe first for the former). Might be worth doing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ccorcos picture ccorcos  路  3Comments

naivefun picture naivefun  路  3Comments

shirotech picture shirotech  路  3Comments

gkatsanos picture gkatsanos  路  4Comments

kocur4d picture kocur4d  路  3Comments