views:

1620

answers:

5

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?

+1  A: 

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.

Matt Chase
A: 

That thing about the urlLoader.dataFormat is absolutely correct.

I accidently used URLLoaderDataFormat.VARIABLES thinking it addressed outgoing data too. Apparently Firefox, Google Chrome and Opera interprate this correctly and are awaiting variables to come BACK before they are firing the Event.COMPLETE. Internet Explorer as always is more relaxed about these things :) Thanx a million!

Martijn
A: 

But I have on problem with this code...If I add a header with this request, event.COMPLETE not firing

    var header:URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream");
ImageURLRequest.requestHeaders.push(header);            

Can some one can help on this?

Peter
A: 

I am getting the following message in all browsers except Chrome

Connection opened PROGRESS: 45/45 Status Changed to 200

In Chrome i am only getting:

Status changed to 0

Any Chrome specific issue?

Thanks, Vivek.

Vivek Lakhanpal
A: 

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.

Vivek Lakhanpal