Actionscript 2 从查询返回 loadvars 的问题

发布于 2024-10-13 11:17:41 字数 1613 浏览 6 评论 0原文

我似乎在对象中有一些无关的字符,我是 Actionscript 的新手,所以我不知道它们是否属于那里,而且我似乎无法提取感兴趣的参数。这是代码段:

_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")
  }

这是跟踪结果:

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

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

~/src/Main.as:53:entered wikiload1。 onLoad 函数

~/src/Main.as:58:wikiload2.displayhtml 未定义

~/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[删除了很多返回值,很长]div%3E%0A&onLoad=%5Btype%20Function%5D

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

为什么 wikiload2.displayhtml 未定义?这是一个 Actionscript 错误,还是我对其调用的脚本(Web 服务器上的 python 脚本)有一些问题。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨轻弹 2024-10-20 11:17:41

无论您的 python 服务器是否有问题,您都应该使用代理嗅探器进行检查,例如 Charles、< a href="http://www.fiddler2.com/fiddler2/" rel="nofollow">Fiddler 甚至 火虫。结果看起来不错,尽管我不确定您在跟踪(wikiload2.toString)中看到的换行符 %0A 是否会导致 loadVars 解析出现问题。首先尝试删除它。

我不确定这是否是解决办法,但您说您是 AS2 新手,所以让我进一步介绍一下您的代码以供将来参考。我假设你必须使用 AS2 而不能使用 AS3 是有原因的。然而,即使使用 AS2,我也强烈建议您研究面向对象编程。一旦完成,构建代码、使用第三方框架以及保持项目的可维护性将变得更加容易。

例如,在框架中,有一个名为 ASAPFramework。它有一个非常简单的对象,称为 LoadVarsXML,它允许您将加载结果作为 XML 对象加载。在 python 和 flash 中都比 loadVars 结构更易于调试。

另一个建议;在 AS2 中,方法调用的范围可以与方法声明的范围不同。换句话说,当您传递该方法以便在 onLoad 等事件回调中执行时,与该方法在同一对象中定义的变量可能无法访问。解决此问题的一种方法是全局存储每个值,就像添加前缀 _root 一样。然而,这会使您的代码难以阅读、难以理解,并且正如您所注意到的,难以调试。 *一般来说,使用 _root 是一个很大的禁忌*。如果您继续扩展上面的示例脚本,您最终会发现自己处于代码意大利面条中,您请求帮助的人都无法理解。

解决这个问题的方法是开始使用类而不是框架脚本,但即使您想坚持使用这些脚本,您也可以通过使用 Delegate.create(scope, method) 包装回调方法来清理一些东西。

以下是您的代码使用 Delegate.create 方法的样子。我还冒昧地将您的 wikiLoad1 和 2 变量重写为 wikiLoader 和 wikiResults。这使得他们的任务更加清晰。

_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");
    }
}

我认为这看起来更干净,因此更容易理解。如前所述,尝试删除 python 中的换行符,如果可以,请尽快查看 LoadVarsXML。它将大大简化 AS2 中的生活。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文