简体   繁体   中英

How do I make sure my ajax calls are always successful?

I have this function that constantly uploads browser data to server by jquery post. Interval is as soon as every second. It seems that some requests don't make it to the server.

The question is how do I make sure every ajax requests are successful. Re-post until it's successful? One thing to consider is data has a high chance of being overwritten by upcoming upload() instances when one instance is known failed. So need to preserve the original data in failed instance too.

function upload(){          
    $.post(
        ajaxURL,
        {history:JSON.stringify(data)}
    );  
}

$post has success, error and complete methods like that;

$.post("example.php", function() {
  alert("success");
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });

taken from JQuery documentation here so you can re-post in error part

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