简体   繁体   中英

xmlhttprequest sending POST twice in 3 minutes interval

I am initiating a python script from webui. This script takes 1 minute to 30 minutes to run. XMLHTTPREQUEST is waiting for response within 3 minutes and if no response it is re-initiating the script.

I want script to run only once. Could you please suggest?

I am using below three lines while initiating the script:

var xhttp = new XMLHttpRequest();
xhttp.open("POST", "/run_script", true);
xhttp.send(datas);

The real issue is that the script is not sending a reliable 200 response, which could happen at the beginning if it runs independently, or after the 90 seconds if the POST request needs the output. I don't know the specifics here, but such a long response time is usually a sign that the work should be handled in the background, with separate requests to start and check on it.

If one really needs to wait right until the timeout, likely the only thing that could be done to prevent this low-level "try again" feature would be to abort the HTTP request right before it happens:

setTimeout(() => {
  if (xhttp.status !== 200) {
    xhttp.abort(); 
    console.log('Aborted before');
  }
}, 179000); // 179 seconds, may need to adjust

Note that this will trigger error events and may not stop already-initiated processes like file uploads. The Fetch API has similar functionality in the form of the recent AbortSignal ; behavior may vary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM