Note that there are some explanatory texts on larger screens.

plurals
  1. POForm Submission vs AJAX Polling Call
    primarykey
    data
    text
    <p>Following up on my question from the other day, I've run into another thing that now I've spent too many hours banging my head against.</p> <p>Mostly, I'm having trouble getting the SUCCESS form to submit. I tried this as well:</p> <p><a href="https://stackoverflow.com/questions/3638750/jquery-form-submit">jQuery form submit</a></p> <p>Here's the code in a semi-functional fiddle:</p> <p><a href="http://jsfiddle.net/ZcgqV/" rel="nofollow noreferrer">http://jsfiddle.net/ZcgqV/</a></p> <p>Essentially what happens is this:</p> <ol> <li>I bind a method to the form's submission via onSubmit (rather than click)</li> <li>On submit, it calls a remote server via jQuery .ajax() call</li> <li>If the response is "PENDING", retry every 1s, nine times</li> <li>On failure, don't submit the form</li> <li>On success, submit the form</li> </ol> <p>No matter what I try, I can't get the form to either submit when I want it to without going into a loop, or not submit immediately while it tries the remote server.</p> <p>~Frustrated-trying-100-things-that-fail-ly yours...</p> <p><em>Here's the code directly in case you dislike fiddles:</em></p> <pre><code>var retries = 0; var success = false; var token = "toki wartooth is not a bumblebee"; $(document).ready(function() { // Attach the action to the form $('#tehForm').attr('onSubmit', 'onsubmit_action(event)'); }); function async(fn) { setTimeout(fn, 1000); } function pollServer() { $.ajax({ type: "POST", cache: "false", url: "/remoteCall", dataType: "json", data: { ref_token: token } }).done(function(data, code, jqXHR) { switch (data.status) { case "SUCCESS": alert("Success"); success = true; // --&gt; HERE IS WHERE I WANT THE FORM TO SUBMIT &lt;-- break; case "PENDING": if (retries &lt; 9) { retries += 1; async(function() { pollServer(); }); } else { alert("Failed after 9 tries"); } break; case "ERROR": alert("Error"); break; default: alert("Some kind of horrible error occurred"); break; } }).fail(function(jqXHR, textStatus) { var statusCode = jqXHR.status; alert("Request failed: " + statusCode + " " + textStatus); }); } function onsubmit_action(event) { pollServer(); if (success === false) { // RETURN FALSE DIDN'T WORK, SO I FOUND THIS event.preventDefault(); } }​ </code></pre> <p>EDIT:</p> <p>Again, the real problem here is that I stop submission of the form. On SUCCESS, I want the form to submit. Currently if I use .submit() in SUCCESS, the AJAX is called again, starting the process over. What I want is the ACTION of the FORM to fire on SUCCESS only.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload