How can I offer an AJAX POST response to a user as a download?

I am trying to enable the vCard export feature on an existing account information page.

Ugly methods include 1, submitting the form to the same page, processing it and re-rendering the entire page, or 2, GET, targeting the iframe on the page. I would really like to avoid both, but I may have to use No. 2 to achieve the goal.

Now I have:

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

Which works in the sense that if I observe XHR activity in Firebug, I see that the request returns with the correct answer, full of data in vCard format. However, it does not ask the user to download the response as a file, even if the card is sent using:

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

Am I doing something wrong, or is it just not possible?

+3
4

, . - :

<input type="image"
       src="/intra/imgs/icons/vcard.png"
       onclick="window.location='/intra/vcard.php?id=992772&type=sponsor';">

vcard.php? , . , $_GET $_POST, GET .

EDIT, , :

<a href="/intra/vcard.php?id=992772&type=sponsor"><img src="/intra/imgs/icons/vcard.png"></a>

, javascript.

+6

, xhr. , , - β„–2, iframe.

, jquery ( , jquery) , this .

+2

, '{$ this- > name_first} {$ this- > name_last}.vcf') http-response firebug! ( , ).

- ?
? http-?

0

, .

0

Source: https://habr.com/ru/post/1706181/


All Articles