I have an AJAX script that I am using to do some database magic after an order is placed on my site.
Here is how I reference it on my order confirmation page:
    <script>
// I define my function
    function voucherRedeem(str) {
      if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
      } else { // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
          document.getElementById("confirmation").innerHTML=xmlhttp.responseText;
        }
      }
      xmlhttp.open("GET","redeemvoucher.php?order="+str,true);
      xmlhttp.send();
    }
    // This grabs the order number in the confirmation field. 
    var finisher = jQuery('div.col-main p a:first').text();
    // This executes the function.
    voucherRedeem(finisher);
    </script>
I'm having cases where the script actually works but I see 500. Other times I see 500 and there are no results. Ultimately, I don't want there to be any 500. This is a staging server with plenty of resources so I don't foresee a network or CPU issue.
The error I'm seeing in Firebug:
GET http://www.website.com/redeemvoucher.php?order=123456 500 Internal Server Error 1.33s
Can I set a delay on this JS function or maybe a document.ready? Again, not really sure what is happening.