PHP 和 Actionscript 之间的通信出现未知错误


Unknown error communicating between PHP and Actionscript

我有一个非常简单或复杂的问题,由你来找出答案。我一直在努力尝试将 URL 加载器类合并到初学者图形程序 - Stencyl。我精通HTML,CSS和PHP,但actionscript对我来说是全新的,所以我真的可以用手使用。这是我得到的:我的域上托管了 4 个文件:

网页.html

样式表.css

请求数据.php

闪存文档.swf

html

和css代码很简单,没有问题,swf文件嵌入在html文档中。Flash 文件是一个简单的表单,其中包含一个文本字段、提交按钮和两个动态文本字段。代码如下:

// Btn listener
submit_btn.addEventListener(MouseEvent.CLICK, btnDown);
// Btn Down function
function btnDown(event:MouseEvent):void {

// Assign a variable name for our URLVariables object
var variables:URLVariables = new URLVariables();
// Build the varSend variable
// Be sure you place the proper location reference to your PHP config file here
var varSend:URLRequest = new URLRequest("http://www.mywebsite.com/config_flash.php");
varSend.method = URLRequestMethod.POST;
varSend.data = variables;
// Build the varLoader variable
var varLoader:URLLoader = new URLLoader;
varLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
varLoader.addEventListener(Event.COMPLETE, completeHandler);
variables.uname = uname_txt.text;
variables.sendRequest = "parse"; 
// Send the data to the php file
varLoader.load(varSend);
// When the data comes back from PHP we display it here 
function completeHandler(event:Event):void{
var phpVar1 = event.target.data.var1;
var phpVar2 = event.target.data.var2;
result1_txt.text = phpVar1;
result2_txt.text = phpVar2;
} 

}

然后我有一个包含以下代码的小 PHP 文件:

<?php
// Only run this script if the sendRequest is from our flash application
if ($_POST['sendRequest'] == "parse") {
// Access the value of the dynamic text field variable sent from flash
$uname = $_POST['uname'];
// Print  two vars back to flash, you can also use "echo" in place of print
print "var1=My name is $uname...";
print "&var2=...$uname is my name.";
}
?>

出于某种原因,这是行不通的。结果只是两个空白文本字段,作为一个动作脚本菜鸟,我不知道发生了什么。任何帮助将不胜感激。谢谢你的时间。

如果您不习惯 AS3,您的问题的答案既简单又令人惊讶。

在 AS3 中,flash.* 类倾向于在使用资源库时创建并存储传递对象的副本。由于它们存储副本,因此在 setter 之后对原始实例的任何修改都不会应用于副本,因此会被忽略。

例如,DisplayObject.filtersContextMenu.customItemsURLRequest.data就是这种情况。

在您的代码中,您在填充variables之前设置varSend.data = variables。你应该做相反的事情:

variables.uname = uname_txt.text;
variables.sendRequest = "parse"; 
varSend.data = variables;
// Send the data to the php file
varLoader.load(varSend);

只有一些班级这样做,即使这样,他们通常也不会对所有的二传手都这样做。