Luca
Luca

Reputation: 1350

catch exception thowns by prototype javascript framework

I'm using the prototype framework to obtain a reverse ajax effect in my page, i'm using this script:

<script language="JavaScript" src="prototype.js"></script>
<script>
Event.observe(window, 'load', function() {
connectToServer();
});

function connectToServer()
{
new Ajax.Updater(
    { success: 'asd', failure: 'error' },
    'evaluation',
    {
        method:     'get',
        onSuccess:  function(transport)
        {
            if (transport.responseText!=''){
                try {
                    eval(transport.responseText)
                } catch (e) {
                    alert(e.message);
                }
            }
            connectToServer();
        },
    });
 }
 </script>

The script works correctlty but i have a problem, when i shutdown the web server (in my case a little web server i wrote in python) a lot of exception are thrown due to error 404, is there a way to catch this exception and block the script?

Upvotes: 2

Views: 249

Answers (1)

Geek Num 88
Geek Num 88

Reputation: 5312

Add an on404 option to the Ajax.Updater options, pass a function handler like you have done for onSuccess.

the on<HTTPSTATUSCODE> handler will prevent the onSuccess from firing only if the HTTP code exists as a handler. ie If the webserver responds with a 500 error and you only have a on404 handler defined, onSuccess might still run, but shouldn't because success is defined as any of the 200s status codes.

Upvotes: 3

Related Questions