为什么我的 URLLoader 在完成后没有调度?
我使用 URLLoader 将一些键/值对发送到 php 脚本,然后将它们转换为电子邮件,发送(或不发送),然后回显带有响应的字符串。
起初它工作正常。 URLLoader 发帖,一分钟后我收到了电子邮件,但由于某种原因我没有收到回复。 事实上,我的 COMPLETE 事件似乎根本没有触发。 这让我很困惑,因为如果我收到电子邮件,我知道我必须正确发送所有内容。 这是我的代码:
public class Mailman{
public static const METHOD:String = URLRequestMethod.POST;
public static const ACTION:String = "mailer.php";
public static var myLoader:URLLoader = new URLLoader();
private static function onMessageProgress(e:Event){
var L:URLLoader = e.target as URLLoader;
Output.trace("PROGRESS: "+L.bytesLoaded+"/"+L.bytesTotal);
for(var k in L){
Output.trace(" "+k+": "+L[k]);
}
}
private static function onOpen(e:Event){
Output.trace("Connection opened");
}
private static function onComplete(e:Event){
Output.trace("Complete!");
}
private static function onStatusChange(e:HTTPStatusEvent){
Output.trace("Status Changed to "+e.status);
}
private static function onMessageFail(e:Event){
PanelManager.alert("ERROR: Could not send your request. Please try again later.");
}
public static function sendMessage(recipient:String,subject:String,message:String){
var _vars:URLVariables = new URLVariables();
_vars.recipient = recipient;
_vars.subject = subject;
_vars.message = message;
var req:URLRequest = new URLRequest(ACTION);
req.data = _vars;
req.method = METHOD;
myLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
myLoader.addEventListener(ProgressEvent.PROGRESS,onMessageProgress);
myLoader.addEventListener(Event.OPEN,onOpen);
myLoader.addEventListener(Event.COMPLETE,onComplete);
myLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS,onStatusChange);
myLoader.addEventListener(IOErrorEvent.IO_ERROR,onMessageFail);
myLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onMessageFail);
myLoader.load(req);
}
public static function test(){
sendMessage("[email protected]","test","this is a test message.");
}
function Mailman(){}
}
当我调用 Mailman.test()
时,我收到的电子邮件完全符合我的预期,这就是跟踪结果:
Connection opened PROGRESS: 45/45 Status Changed to 0
这是怎么回事? 如果我正确理解文档,当我开始下载响应时就会发生 Open 事件,并且显然这种情况正在发生,那么我怎样才能取回 http 状态 0? 有任何想法吗?
I'm using a URLLoader to send a few key/value pairs to a php script, which then turns them into an e-mail, sends it (or not), and then echoes a string with a response.
At first it works fine. The URLLoader posts, and I get my e-mail a minute later, but for some reason I'm not getting my response back. In fact, my COMPLETE event doesn't seem to fire at all. This is confusing me because if I'm getting my e-mail, I know I must be sending everything properly. Here's my code:
public class Mailman{
public static const METHOD:String = URLRequestMethod.POST;
public static const ACTION:String = "mailer.php";
public static var myLoader:URLLoader = new URLLoader();
private static function onMessageProgress(e:Event){
var L:URLLoader = e.target as URLLoader;
Output.trace("PROGRESS: "+L.bytesLoaded+"/"+L.bytesTotal);
for(var k in L){
Output.trace(" "+k+": "+L[k]);
}
}
private static function onOpen(e:Event){
Output.trace("Connection opened");
}
private static function onComplete(e:Event){
Output.trace("Complete!");
}
private static function onStatusChange(e:HTTPStatusEvent){
Output.trace("Status Changed to "+e.status);
}
private static function onMessageFail(e:Event){
PanelManager.alert("ERROR: Could not send your request. Please try again later.");
}
public static function sendMessage(recipient:String,subject:String,message:String){
var _vars:URLVariables = new URLVariables();
_vars.recipient = recipient;
_vars.subject = subject;
_vars.message = message;
var req:URLRequest = new URLRequest(ACTION);
req.data = _vars;
req.method = METHOD;
myLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
myLoader.addEventListener(ProgressEvent.PROGRESS,onMessageProgress);
myLoader.addEventListener(Event.OPEN,onOpen);
myLoader.addEventListener(Event.COMPLETE,onComplete);
myLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS,onStatusChange);
myLoader.addEventListener(IOErrorEvent.IO_ERROR,onMessageFail);
myLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onMessageFail);
myLoader.load(req);
}
public static function test(){
sendMessage("[email protected]","test","this is a test message.");
}
function Mailman(){}
}
When I call Mailman.test()
, I get my e-mail exactly like I expect, and this is what traces out:
Connection opened PROGRESS: 45/45 Status Changed to 0
How can this be? If I'm understanding the documentation properly, the Open event happens when I begin downloading my response, and clearly that is happening, so how can I get back an http status of 0? Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了。
问题出在 URLLoader 的 dataFormat 上。 这是您返回的格式,而不是您发送的格式。 我将其切换为
URLLoaderDataFormat.TEXT
并且它工作得很好。I found it.
The problem was with the URLLoader's dataFormat. This is the format for what you're getting BACK, not what you're sending. I switched it to
URLLoaderDataFormat.TEXT
and it worked perfectly.发生这种情况的另一个原因是 - 如果您在注册事件侦听器时使用弱引用,并且不保留对 URLLoader 实例和处理事件的实例的引用,GC 可能会在它们接收任何事件之前清理它们。
Another reason this could happen - if you use weak references when registering your event listeners, and do not keep a reference to your URLLoader instance and the instance handling the event(s), GC may clean them up before they can receive any events.
好吧,我在我的例子中找到了答案,它是 .NET 页面,它导致 Chrome 中的 Status=0。 在写入要发送回 Flash 的响应后,我们在 .net 页面中所做的事情是,我们关闭了正在重置页面的响应对象,因此 Chrome 显示 status=0 并且无法呈现结果。 注释掉 response.close 行后,它开始正常工作。
我在 http 上写了我对此问题的经验以及如何解决它://viveklakhanpal.wordpress.com/2010/07/01/error-2032ioerror/
谢谢,
维韦克。
Ok I found the answer in my case it was .NET page which was responsible for Status=0 in Chrome. What we were doing that in .net page at the line after writing response to be sent back to flash, we were closing the response object which was reseting the page and because of which Chrome was showing status=0 and couldn't render the result. After commenting out the response.close line it started working fine.
I wrote my experience with this problem and how i was able to solve it at http://viveklakhanpal.wordpress.com/2010/07/01/error-2032ioerror/
Thanks,
Vivek.