如何使用$ .ajax()GET响应指定内容类型和内容处置 [英] How to specify content-type and content-disposition with $.ajax() GET response

查看:176
本文介绍了如何使用$ .ajax()GET响应指定内容类型和内容处置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发出一个异步GET请求,该请求返回一个具有MIME内容类型的文档,并使其带有浏览器的保存"对话框.

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.

以前,我曾经通过链接进行常规HTTP(非异步)调用,并且返回的响应具有"Content-Type"和"Content-Disposition",如下所示:

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";

是否可以将其转换为jQuery的$ .ajax()GET请求?

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

$.ajax方法仅支持dataTypes,"xml","html","script","json","jsonp"和"text".我的响应数据类型会属于这些类别之一吗?

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?

我的请求如下:

$.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 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.

推荐答案

据我所知,没有办法获得真正的Ajax请求以弹出一个保存对话框.这与服务器发送的标头无关.

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.

如果要以编程方式弹出一个保存对话框,则可以使用jQuery将隐藏的iframe附加到带有src的URL的页面上.这将在必要时弹出对话框.

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.

这篇关于如何使用$ .ajax()GET响应指定内容类型和内容处置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆