Vue.js Vue-Resource Detect Timeout and resend post

2.5k views Asked by At

Using Vue Resource with Vue.js v1 This is also running using vue-router and built with Browserfy.

I have vue-resource working on the whole for post and gets but I have one particular one which occasionally times out or so it seems. I confirm the server is getting the post request and then sending the response back but the webpage sits waiting as if it's missed the response. It seems to happen randomly. If i reload the page and resend then it works. I've tried to replicate by pausing the server before the sending of the response, wait a bit then continue. In that case it works everytime and webpage continues as expected.

my post functions is as follows (slightly edited to shrink so easier to read)

 this.saving = true;
 // POST save to the database
 this.$http.post('/api/savebr',
          {
            tNumber: parseInt(this.tNumber),
            bNumber: parseInt(this.cNumber)
          }, 
          // I added the timeout for testing
           { timeout: 2000 }
      ).then((response) => {

        this.completed.push(parseInt(this.cNumber));
        this.saving = false;

      }, (response) =>
      {
        // error callback
        console.log(response);
        this.saving = false;
        alert('Could not save to server. Please try again.');
      })
    }

In above the timeout works if I pause the server. When the server continues the webpage ignores the response which I guess is correct. When the webpage randomly misses the response the error callback is never done. It works if I alter response from server to be an error which is expected too.

The timeout option I added recently to see if that makes any odds and also tried with zero setting.

Is it possible to detect the timeout, cancel the current post and send a new one?

If I repeat the post, suddenly both seem to work and so the success code runs twice even though originally the first request seems to be stuck.

Sorry for long post and all pointers gratefully received.

0

There are 0 answers