So far we have seen how to trap the javascript errors in Part 1. Now we have
the data. We will see now, how to send data to the server using AJAX.

Send Data to Server

ajaxCtrl(
    function(){
        return true;
    },"ajxerrorLogger.php",theData
);

The remote file is “ajxerrorLogger.php”.

I?ve used PHP for this.
Instead you can use ASP, JSP also.

This is used to receive the
data sent from the JavaScript error handling function. That’s it.
Our JavaScript errors are now sent to the server silently.

What Next? In the server side, we have to code the “ajxerrorLogger.php”

Receive Data and Write into a File

if($_POST && $_POST['file']!=''){
    $filename = "./errlogs.txt";
    $fh = fopen($filename,"a+");

    //the content is in the form
    //Date    File    LineNo    Error Message
    //(tab delimited)
    $fcontent = date("d/m/Y h:i:s", mktime())."\t".
    $_POST['file']."\t".$_POST['line']."\t".$_POST['err']."\r\n";
    if (is_writable($filename)) {
        if (fwrite($fh, $fcontent) === FALSE) {
        }
        fclose($fh);
    }

}

$fh = fopen($filename,"a+");

 Read the rest of this entry »