如何在下载时向用户提供Ajax POST响应?

Tre*_*ble 3 php jquery download

我正在尝试在充满帐户信息的现有页面中包含vCard导出功能.

丑陋的方法将涉及1,将表单提交到同一页面,处理它并重新呈现整个页面,或2,GET以页面上的iframe为目标.我真的想避免这两个,但我可能不得不使用#2来实现目标.

现在我有:

<input type="image" src="/intra/imgs/icons/vcard.png" onclick="$.post('/intra/vcard.php', { id: '992772', type: 'sponsor'});">
Run Code Online (Sandbox Code Playgroud)

从某种意义上讲,如果我在Firebug中观看XHR活动,我会看到请求返回正确的响应,充满了vCard格式的数据.但是,它不会提示用户将响应作为文件下载,即使该卡是通过以下方式发送的:

header('Content-Type: text/x-vcard');
header("Content-Disposition: attachment; filename={$this->name_first}{$this->name_last}.vcf");
Run Code Online (Sandbox Code Playgroud)

我做错了什么,或者这是不可能的?

Pao*_*ino 6

我很困惑究竟是什么问题.为什么不做这样的事情:

<input type="image"
       src="/intra/imgs/icons/vcard.png"
       onclick="window.location='/intra/vcard.php?id=992772&type=sponsor';">
Run Code Online (Sandbox Code Playgroud)

然后返回相应的下载标题vcard.php?当浏览器获得这些内容时,它将保留在同一页面上并提示下载.您将不得不更改代码来处理变量,$_GET而不是$_POST你应该使用GET.

编辑在评论中指出,这样做更合适:

<a href="/intra/vcard.php?id=992772&type=sponsor"><img src="/intra/imgs/icons/vcard.png"></a>
Run Code Online (Sandbox Code Playgroud)

从那时起,禁用javascript的用户就可以访问它.