views:

326

answers:

2

Hi all, trying to make a page which will recursively call a function until a limit has been reached and then to stop. It uses an ajax query to call an external script (which just echo's "done" for now) howver with neither onSuccess or onFailure triggering i'm finding it hard to find the problem.

Here is the javascript for it. In the header for the webpage there is a script to an ajax.js document which contains the request data. I know the ajax.js works as I've used it on another website

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>National Lettings Inventories</title>
    <link   type="text/css"       href="/inv/default.css" rel="stylesheet" />
    <script type="text/javascript" src="/inv/includes/swfobject.js"></script>
    <script type="text/javascript" src="/inv/includes/ajax.js"></script>
    <script type="text/javascript">
        swfobject.registerObject("myFlashContent", "9.0.0");
    </script>
</head>
<body onload="start()">
    <div id="topBar">
        <div class="logo">            
        </div>
    </div>
    <div id="Body">
<script type="text/javascript">
<!--

var rooms = 1;
var items = 0;
var ccode = "9999/1";

var x = 0;

function echo(string,start){
    var ajaxDisplay = document.getElementById('ajaxDiv');
    if(start)
        {ajaxDisplay.innerHTML = string;}
    else
        {ajaxDisplay.innerHTML = ajaxDisplay.innerHTML + string;}
}

function locations()
{
    echo("Uploading location "+x+" of " + rooms,true);
    Ajax.Request("Perform/location.php",
    {
        method:'get',
        parameters: {ccode: ccode, x: x},
        onSuccess: function(reply)
        {alert("worked");
            if(x<rooms)
            {
                x++;
                locations();
            }
            else
            {
                x=0;
                echo("Done",true);
            }
        },
        onFailure: function()
        {alert("not worked");
             echo("not done");
        }
    });
    alert("boo");
}

function start()
{locations();}
//-->
</script>

Uploading

<div id='ajaxDiv'>

</div>
        </div>
                <div class="link" id="bottom">
            <a href="index.php" ><img src="/inv/images/back.gif" class="link" alt="BACK"/></a>
        </div>
                <div id="bottomBar">
            <p>Copyright © 2010 National Lettings Inventories</p>
        </div>
     </body>
</html>

Opera Error Console

JavaScript - http://localhost/inv/Upload/upload.php
Event thread: load
Error:
name: TypeError
message: Statement on line 89: Cannot convert undefined or null to Object
stacktrace: n/a; see  opera:config#UserPrefs|Exceptions Have Stacktrace

Any help or advice will be most appreciated.

EDIT: Added the whole page source code, theres no line 89 here though

A: 

Any help or advice will be most appreciated.

OK ;-)

The first bit of advice I would give you is that 'I know the ajax.js works as I've used it on another website' is a self-inflicted blind spot.

Second, although it might seem to give you valid results, you should either declare Rooms as a number, e.g. var Rooms = 1; or cast it to an int before comparison e.g. x < parseInt(Rooms,10);

Third, it is a matter of style, but typically local variables are camel cased (myVar) while namespaces and functions/classes that are meant to be instantiated are pascal cased (Room).

Now, to the problem: the code posted, other than the above mentioned, appears to be valid. So this leaves the conclusion that Ajax.Request is silently failing either due to a defect in that script or in the calling convention. (see advice #1)

With the information given, that is as far as I can take this answer.

Good luck.

Sky Sanders
Ops, removed the "". Forgot to check them when i copied it from the last working script i used these functions in. Didn't fix it though :(
Kye
@Kyle, Well, since you script seems to parse ok and you are getting to the initial ajax call, something is definitely going very wrong in Ajax.Request as your onFailure doesn't get called. Try using firebug and take a look at the console for reported errors.
Sky Sanders
JavaScript - http://localhost/inv/Upload/upload.phpEvent thread: loadError:name: TypeErrormessage: Statement on line 89: Cannot convert undefined or null to Objectstacktrace: n/a; see opera:config#UserPrefs|Exceptions Have StacktraceMean anything?
Kye
@Kyle - Yes, it means something is broken. ;-). Something is null that shouldn't be. So you are getting closer. Look on line 89.
Sky Sanders
Theres no line 89 though.
Kye
@Kyle, I am sure there is a line 89 in prototype.js. Maybe there. This is simple debugging and we can't do it in this forum. You will just have to keep snooping and use the clues you find. You will get it.
Sky Sanders
lines 88-90 from prototype script function klass() { this.initialize.apply(this, arguments); }Would this be the cause of the problem?
Kye
A: 

I don't notice anything obviously wrong with your code*. But the fact that last alert (after the call) doesn't fire is a pretty good indicator that the call caused the javascript engine to fail. Have you tried using firebug (or similar) and see what happens on the console?

The onSuccess and onFailure callback will only be called if the call is actually fired. If the script fires an exception, or (since javascript is usually interpreted directly) encounters a syntax error etc. the script engine will usually stop running the script and pop a error message to the javascript console (wich is not allways obviously indicated by the browser). If the script stops before or during the call the callbacks of course won't fire.

EDIT: Sounds like you accidentally use an uninitialized variable, somehow. Try double checking that you've typed all variables correctly (including case). Not really sure what's happening as I cannot see what's on line 89...

EDIT: I just noticed, you've written Ajax.Request(... not new Ajax.Request(... as in the tutorial on the prototype site. Not sure if it matters, though...

(* Though the uppercase letter in the path looks a bit suspicious; not really wrong, but might look like you accidentally uppercased the first letter. Usually best not to require uppercase in url's in case some browser munges it.)

Stein G. Strindhaug
I capitalize folder names and never the files. Just helps me add distinction to what is what.
Kye
JavaScript - http://localhost/inv/Upload/upload.phpEvent thread: loadError:name: TypeErrormessage: Statement on line 89: Cannot convert undefined or null to Objectstacktrace: n/a; see opera:config#UserPrefs|Exceptions Have Stacktracemean anything?
Kye
I took the new out because jslint told me to :P
Kye
Bah, jslint was wrong XDAdded it back in and got the alerts though. Thanks.
Kye
Yeah, jslint isn't very happy about prototypeJS syntax... ;)
Stein G. Strindhaug
Now to try to make it call the second function when the first finishes.
Kye