Why is the URLLoader not dispatching when it completes

actionscript-3urlloader

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("john@example.com","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?

Best Answer

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.

Related Topic