From our security doc:
And the relevent piece of the code which fetches the CSRF code before performing the request:
https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/lib/ajax.js.es6#L149-L166