Search code examples
flashactionscriptactionscript-2loadvars

Actionscript 2 problem with return of loadvars from query


I seem to have some extraneous characters in the object, I'm new to Actionscript, so I don't know if they belong there or not, and I can't seem to extract the parameter of interest. Here's the code segment:

_root.buttonBkg.onPress = function() {
   var wikiload1:LoadVars = new LoadVars();
   var wikiload2:LoadVars = new LoadVars();
   wikiload1.wikiname = wikiterm;
   wikiload2.onLoad = function(success:Boolean) {
    trace("entered wikiload1.onLoad function")
    if (success) {
     _root.createTextField("wikidisplay", _root.getNextHighestDepth(), 0, 150, 550, 380);
     _root.wikidisplay.html = true;
     _root.wikidisplay.htmlText = wikiload2.displayhtml;
     trace("wikiload2.displayhtml is" + wikiload2.displayhtml);
     trace(wikiload2.toString());
     trace("success route");
    } 
    else {
     _root.createTextField("wikidisplay", _root.getNextHighestDepth(), 0, 150, 550, 380);
     _root.wikidisplay.text = "Error connecting to server.";
     trace("error route");
    }
   };
   wikiload1.sendAndLoad("http://myURL.script.py", wikiload2, "POST");
   trace("?wikiname=" + wikiload1.wikiname);
   trace("did the sendAndLoad")
  }

Here's the trace results:

~/src/Main.as:70:?wikiname=coffee

~/src/Main.as:71:did the sendAndLoad

~/src/Main.as:53:entered wikiload1.onLoad function

~/src/Main.as:58:wikiload2.displayhtml isundefined

~/src/Main.as:59:%0Adisplayhtml=%3Cp%3E%3Cb%3ECoffee%3C%2Fb%3E%3C%2Fp%3E%3Cp%3E%3Cbr%2F%3E%3Cbr%2F%3ECoffee%20is%20a%20brewed%20drink%20prepared%20[MUCH RETURNED VALUE DELETED, IT'S LONG]div%3E%0A&onLoad=%5Btype%20Function%5D

~/src/Main.as:60:success route

Why is wikiload2.displayhtml undefined? Is this an Actionscript error, or to I have some problem with the script it's calling (a python script on a web server).


Solution

  • Whether or not you have a problem with the python server, you should check with a proxy sniffer, like Charles, Fiddler or even FireBug. The result looks fine, although I'm not sure if the newline character %0A you see in the trace(wikiload2.toString) is causing problems with the loadVars parsing. Try to remove that first.

    I'm not sure this is the fix, but you say you're new to AS2 so let me go a bit further with your code for future reference. I'm assuming there's a reason you have to be in AS2 and can't work with AS3. However, even with AS2 I'd urge you to look into object oriented programming. Once you do, it will become much easier to structure your code, work with 3rd party frameworks and keep a level of maintainability in your projects.

    In frameworks for example, there's one called ASAPFramework. It has a very easy object called LoadVarsXML, that will let you load the result of your load as an XML object. Better to debug than the loadVars structure both in python and in flash.

    Another advice; In AS2, the scope of a method call can be another one than the scope of the method declaration. In other words, variables that are defined in the same object as the method, can be inaccessible when you pass that method along for execution in event callbacks like onLoad. One way to work around this is by storing every value globally, like you do with prefixing _root. This however, makes your code hard to read, hard to understand and as you've noticed, hard to debug. *The use of _root is in general, a big no-no*. If you keep expanding your example script above, you'll eventually find yourself in a code spaghetti that no-one you ask to help will be able to understand.

    The way around this is by starting to use classes instead of frame scripts, but even if you want to stick to those, you can already clean things up a bit by wrapping your callback methods with Delegate.create(scope, method).

    Here's how your code would look like making use of the Delegate.create method. I also took the liberty of rewriting your wikiLoad1 and 2 variables to wikiLoader and wikiResults. This makes it a bit clearer what their tasks are.

    _root.buttonBkg.onPress = Delegate.create(this, handleButtonPress);
    
    function handleButtonPress():Void {
        var wikiResult:LoadVars = new LoadVars();
        wikiResult.onLoad = Delegate.create(this, handleWikiLoad);
    
        var wikiLoader:LoadVars = new LoadVars();
        wikiLoader.wikiname = wikiterm;
        wikiLoader.sendAndLoad("http://myURL.script.py",wikiResult,"POST");
        trace("?wikiname=" + wikiLoader.wikiname);
        trace("did the sendAndLoad");
    }
    
    function handleWikiLoad(success:Boolean):Void {
        trace("entered wikiLoader.onLoad function");
        if (success) {
            _root.createTextField("wikidisplay",_root.getNextHighestDepth(),0,150,550,380);
            _root.wikidisplay.html = true;
            _root.wikidisplay.htmlText = wikiResult.displayhtml;
            trace("wikiResult.displayhtml is" + wikiResult.displayhtml);
            trace(wikiResult.toString());
            trace("success route");
        } else {
            _root.createTextField("wikidisplay",_root.getNextHighestDepth(),0,150,550,380);
            _root.wikidisplay.text = "Error connecting to server.";
            trace("error route");
        }
    }
    

    I think this looks much cleaner and is therefore easier to understand. As said, try to remove the newline character in python, and if you can, check out the LoadVarsXML from ASAP. It will seriously simplify life in AS2.