AJAX requests are processed in the background, so the download headers will be ignored and only passed to your callback.
If you want to force download the file without redirecting him to the page you can
- inject an iframe to the page and pass the download url as src-parameter
- link to the download url with an a-tag and add the HTML5 download-attribute to it
- put the result of your ajax-request base64-encoded in an injected a-tag as src parameter and pass a download-parameter.
Example for downloading the url https://stackoverflow.com/ as "stackoverflow.html": <a href="https://stackoverflow.com/" download="stackoverflow.html">Click me!</a>
Example for downloading the text "test" as "file.txt":
<a href="data:text/plain;base64,dGVzdA==" download="file.txt">Click me</a>
(where dGVzdA==
is "test" base64-encoded)
After injecting both a-tags you can use e.g. jQuery to simulate an user clicking on them.