Why is my URLLoader not dispatching when it completes?
Asked Answered
L

3

7

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?

Lapham answered 2/3, 2009 at 20:51 Comment(0)
L
4

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.

Lapham answered 2/3, 2009 at 21:30 Comment(0)
A
1

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.

//make sure the URLLoader and onComplete instances are not local vars
var req:URLRequest = new URLRequest("dosomething.php");
myLoader.addEventListener(Event.COMPLETE, onComplete, false, 0, TRUE);
myLoader.load(req);
Adrianadriana answered 10/11, 2010 at 23:51 Comment(1)
I was experiencing this exact same problem after having found your post, thanks a lot! In fact, using weak references isn't always a good pratice ^^...Saltire
P
0

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.

Phytogeography answered 1/7, 2010 at 11:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.