4

I am building a website that communicates via javascript with a webservice that accepts posted json requests. I Post these requests using XMLHttpRequest. In some cases the service returns a download response:

Example:

Content-Disposition: attachment; filename="somefile.txt"
Content-Type: application/force-download
Content-Length: 3

123

if document.location is pointed to an URL that serves a response like the one above, the browser will automatically prompt the user with a file download dialog.

My question is: Can I achieve the same effect with XMLHttpResponse objects somehow? is there an Iframe trick or something? Unfortunately there is no target member on XMLHttpRequest objects like there is on html forms, cause if I could just redirect the full response (with headers) to a new page or hidden iframe the problem would be solved, I guess.

1 Answers1

0

I guess not. But you can try submitting hidden form to a hidden iframe.

NiematojakTomasz
  • 2,433
  • 20
  • 23
  • That was my thought too, at first, but forms are pretty un-flexible in regards to Content-Type which can only be application/x-www-form-urlencoded or multipart/form-data. Unfortunately the webservice in the other end is expecting application/json. A form in an iframe would work if I could control the format - simply send raw data. – Jakob Simon-Gaarde Sep 18 '11 at 22:17