How to specify content type and content location with $ .ajax () GET response

I would like to make an async GET request that will return a document with the MIME content type and make it display the browserโ€™s Save dialog.

I used to make a normal HTTP call (non-asynchronous) by reference, and the response returned had "Content-Type" and "Content-Disposition", for example:

Content-Type: text/plain Content-Disposition: attachment; filename=genome.jpeg; modification-date="Wed, 12 Feb 1997 16:29:51 -0500"; 

Is there a way to convert this into a jQuery $ .ajax () GET request?

The $ .ajax method only supports data types: "xml", "html", "script", "json", "jsonp" and "text". Will my response data type fall into one of these categories?

My query looks like this:

 $.ajax({url: myUrl, data: params, type: "GET", success: function(data) { console.log("try to save this file!"); }, error: function(req, status, errThrown){ alert("ERROR: Something happened"); } 

In the success callback, I see that the contents of the file are passed in the data variable as plain text, but the save dialog box is required to open in the browser.

The server sends a response with the correct set of headers.

+1
source share
2 answers

I don't know how to get a true Ajax request to open a save dialog. This has nothing to do with the headers sent by the server.

If you want to open the save dialog box programmatically, you can use jQuery to add a hidden iframe to the page with an url in the form of src. If necessary, a dialog box should appear.

+2
source

The server has to do something. There is nothing you can do about it that will cause an unwanted server to set response headers.

-1
source

All Articles