0

I am attempting to submit a form to an external site which is prepared to receive it, but there were some points when the external site was done and caused my site to be stuck waiting with no response just using form.submit()

I wrote the following to try to handle this, but the timeout is not being thrown.

$.ajax({
      async: false,
      url: url,
      data: $('#tppForm').serialize(),
      type: 'POST',
      timeout: 5000,
      statusCode: {
         404: function() {
            console.log('Site Not Found');
         },
         500: function() {
            console.log('Site Down');
         }
      },
      error: function() {
         console.log('Timeout');
      }
});
Codermonk
  • 883
  • 16
  • 32

1 Answers1

1

I checked both the jQuery documentation as well as other sources, the behavior of timeout: is not clearly specified for async:false.

My suggestion is to change this to async:true to better handle the unpredictable nature of remote (network) calls. See jquery ajax() async false and jQuery ajax success anonymous function scope .

This will change the challenge to one where your client side (browser) must have a "ready" state flag that can indicate to the user they should wait and block overlapping calls. Personally I like to alter the submit button text / color for user feedback. Just prior to making your AJAX call you should also check the state flag to ensure there are no pending operations.

Community
  • 1
  • 1
Shawn C
  • 356
  • 2
  • 5
  • Thanks for finding out about Timeout for me. I won't be able to see async as the User actually needs to complete some details on the external site before being returned to my web app. Was trying to add a fallback in case the external site was down and my web apps form would get stuck as it couldn't submit anywhere. – Codermonk Apr 10 '14 at 16:17
  • Site to site interaction is a much deeper question than what you've posited here. I suggest you ask a new question if you are still looking for suggestions. – Shawn C Apr 14 '14 at 16:49
  • Thanks, i will do more research on the subject first, I would upvote but I don't have the reputation required yet. – Codermonk Apr 14 '14 at 18:00