简体   繁体   中英

catching errors in ajax scripts with php

In an effort to make debugging easier, I introduced an error handler into my PHP code that collects all errors, warnings, etc. and logs database queries and displays them at the end of the page when an administrator is logged in to the site.

I'd like to do something similar for ajax queries. And more generally I would like to integrate the two error panels together, but I am at a loss for a good method to do it.

The rough idea of what I want is a popup panel with errors on the page including errors in asynchronous scripts.

I already have a way of making popup panels and filling them with data from ajax requests.

I thought of using the comet pattern for the error window and registering an exit handler with php that sends a http request that passes the errors to the window somehow, but I am not quite sure of how to go about doing this and how to tie it all to the php session(so that user don't see other's errors for example.)

Any ideas how one might go about this? Is there something simple that I am overlooking?

Ok, what I ended up doing, and it seems to work pretty well is:

I wrote a php function that collects all the errors on a page and outputs a specially formatted div with class="systemError". it contains a header with the filename it was called from and a div containing the error list. Lets say this function is called "xx_error_get($filename)"

Then I had each ajax script return this string as part of its results. All I did was add a call

echo xx_error_get(__FILE__);

at the end of the returned XML object.

Back in my main page's php, I added a hidden div to my body with id="systemTemp"

then I added a few lines to the $(document).ready handler in jQuery:

    $(document).ajaxSuccess(function(e, xhr, settings) {
                    $("#systemTemp").html(xhr.responseText);
                    $("#systemError").prepend($("#systemTemp").children(".systemError").html());
                    $(".systemError").remove();
                    $("#systemTemp").empty();
                    $("#systemError").accordion("destroy");
                    $("#systemError").accordion({autoHeight:false});            
    });

Not exactly the cleanest code, but I hope this is helpful to anyone.

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