开发者

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

开发者 https://www.devze.com 2023-01-04 08:33 出处:网络
I would like to make an async GET request that returns back a document with MIME content type and cause it to bring the browser\'s \'Save\' dialog.

I would like to make an async GET request that returns back a document with MIME content type and cause it to bring the browser's 'Save' dialog.

Previously, I used to make a regular HTTP (non-async) call through a link and the returned response had a 'Content-Type' and 'Content-Disposition' like so:

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 to jQuery's $.ajax() GET request?

The $.ajax method only supports the dataTypes, "xml", "html", "script", "json", "jsonp", and "text". Would my response data-type fall into one of these categories?

My request looks like this:

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

In the 'success' callback, I see the file contents passed back in the 'data' variable as a plain text but need the 'save' dialog to get launched on the browser.

The server IS sending back the response with the correct headers set.


There is no way I know of to get a true Ajax request to pop a save dialog up. This has nothing to do with the headers sent by the server.

If you'd like to programatically pop a save dialog box, you can use jQuery to append a hidden iframe to the page with the URL as it's src. This should pop the dialog box as necessary.


That's something the server has to do. There's nothing you can do from the client side that will force an unwilling server to set response headers.

0

精彩评论

暂无评论...
验证码 换一张
取 消