开发者

Ajax Request not working. onSuccess and onFailure not triggering

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">
<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;
v开发者_如何转开发ar 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


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.)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜