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?
Webpack's url-loader lets you import arbitrary files, like images. If you import a . png file, url-loader will ensure that import resolves to a Base64 string representing the contents of the file.
Webpack url-loader jpg file inline.
The html-loader will parse the URLs, require the images and everything you expect. The extract loader will parse the javascript back into a proper html file, ensuring images are required and point to proper path, and the asset modules will write the .html file for you. Example: webpack.config.js module.
Loaders can transform files from a different language (like TypeScript) to JavaScript or load inline images as data URLs. Loaders even allow you to do things like import CSS files directly from your JavaScript modules!
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With