Handle file download from ajax post

前端 未结 20 2601
心在旅途
心在旅途 2020-11-21 05:46

I have a javascript app that sends ajax POST requests to a certain URL. Response might be a JSON string or it might be a file (as an attachment). I can easily detect Content

20条回答
  •  情深已故
    2020-11-21 06:11

    Here is my solution using a temporary hidden form.

    //Create an hidden form
    var form = $('
    ', {'method': 'POST', 'action': this.href}).hide(); //Add params var params = { ...your params... }; $.each(params, function (k, v) { form.append($('', {'type': 'hidden', 'name': k, 'value': v})); }); //Make it part of the document and submit $('body').append(form); form.submit(); //Clean up form.remove();

    Note that I massively use JQuery but you can do the same with native JS.

提交回复
热议问题