Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8658523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:47:18+00:00 2026-06-12T15:47:18+00:00

I have a button [Download Document] which does these two things: Makes an ajax

  • 0

I have a button [Download Document] which does these two things:

  1. Makes an ajax call and populates a table with data.
  2. Post submits a form which:

    a. Runs a back-end script that creates a document

    b. Returns the document to the browser in a way that the browser downloads the document.

One problem. The post submit is not ajax, which means that the browser treats it the same way as clicking on a link, refreshing a page, or closing the window, e.g. it cancels the ajax request without even getting a response error.

Using .preventDefault() on the button doesn’t work. It does allow the ajax call (1) to complete successfully, but then the form isn’t submitted, so (2) the form is never submitted.

    <form action="/backendScript" method="post">
      <input type="submit" value="Download Document">
    </form>

    $(".downloadBtn").on("click", function (e) {
        generateTable();
        e.preventDefault(); // without this, the ajax in generateTable(); is cancelled, but with it, the form isn't submitted.
    });

Doing (2) with an ajax post also doesn’t work. It can complete (2.a), but the returned document goes straight to Javascript, instead of the browser, and Javascript isn’t able to cause a download event, so (2.b) fails with this approach.

   $.post("/backendScript", formData, function (generatedDoc) {
        console.log(generatedDoc); //yay, here's our doc! but how do we download it??
    });

So it looks like I must post submit the form. But how can I prevent this from canceling any ajax requests that are in progress?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:47:20+00:00Added an answer on June 12, 2026 at 3:47 pm

    In the success/error/whatever response event you want from your ajax call in generateTable, submit the form. Either document.forms["myform"].submit(); or $('#myform').submit(); (depending on if you want to use vanilla js or jQuery).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my .aspx page, I have download button which onclick download the .apk file.
Inside a webpage I have an Excel download button, which redirects to a webpage
I have a form which allows to view data in a page or download
I have a button called Download page at the bottom of my detailView of
I have button which post values of form to url but during post i
I have button, which fires an event, that deletes a record from the database.
I have a link: <a class=fg-button ui-state-default fg-button-icon-left assistButton pbcType=download title=Click for download information><span
I have this JavaScript function which counts down until you can click the download
I have a web-page with complicated form. Also, there is a button preview, which
Im trying to get a website to have a button that forces a download

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.