0

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.

pnuts
  • 58,317
  • 11
  • 87
  • 139
sparecycle
  • 2,038
  • 5
  • 31
  • 58
  • Why did you tag this question [jquery] if you're not going to use it? http://api.jquery.com/category/ajax – Blazemonger Sep 26 '14 at 17:59
  • I'll remove the tag, I use jQuery towards the end of the document to select an element's .text. – sparecycle Sep 26 '14 at 18:00
  • Still doesn't explain why you don't just use `$.get('redeemvoucher.php',{order:str},function(data) { /* success */ })`. – Blazemonger Sep 26 '14 at 18:01
  • Thank you for the link, I will look into using it. I am currently implementing AJAX based on the tutorial provided here:http://www.w3schools.com/php/php_ajax_database.asp. I prefer using jQuery where I can but am new to AJAX. – sparecycle Sep 26 '14 at 18:04
  • If possible , can post `php` ? Thanks – guest271314 Sep 26 '14 at 18:04
  • Relying on w3schools.com is your first error, then. It's widely considered to be unreliable and poorly-maintained. – Blazemonger Sep 26 '14 at 18:05
  • @Blazemonger Thanks for the heads up, I will check out the documentation you provided. – sparecycle Sep 26 '14 at 18:06
  • 2
    Try hitting the url http://www.website.com/redeemvoucher.php?order=123456 directly on your test server with error reporting turned on. Looks like a PHP error causing the 500 response. – Shadow Radiance Sep 26 '14 at 18:07
  • 500 is not going to be with the clientside, look at the error message on the server. – epascarello Sep 26 '14 at 18:07

2 Answers2

2

You have error in your PHP i think.

To catch it try to use this (at top of your redeemvoucher.php):

function myErrorHandler($errno, $errstr, $errfile, $errline) {
    echo $errno."\n".$errstr."\n".$errfile."\n".$errline;
}

function myFatalErrorShutdownHandler() {
    $last_error = error_get_last();
    if ($last_error['type']===E_ERROR) {
        myErrorHandler(E_ERROR, $last_error['message'], $last_error['file'], $last_error['line']);
    }
}


set_error_handler('myErrorHandler');
register_shutdown_function('myFatalErrorShutdownHandler');
Eugene
  • 547
  • 4
  • 11
  • I was able to tail -f my error.log and it was indeed mentioning an error. Apparently my PHP file that I am calling shouldn't be using this: $results = $readConnection->fetchAll($voucherattachquery); Error: http://stackoverflow.com/questions/12979510/pdo-error-sqlstatehy000-general-error-when-updating-database – sparecycle Sep 26 '14 at 18:16
0

Don't rely on the examples at w3schools.com -- they're widely considered to be unreliable and poorly-maintained. Instead, just use $.get to do the hard work:

$.get('redeemvoucher.php',{'order':str})
    .done(function(data) {
       /* code you put in here will run after the AJAX returns successfully */ 
    }).fail(function() {
       /* code you put in here will run after the PHP script returns an error */ 
    });

http://api.jquery.com/category/ajax

Blazemonger
  • 90,923
  • 26
  • 142
  • 180