views:

274

answers:

2

Hello,

I have an issue with my eventListeners with the URLLoader, but this issue happens in IE, not in FF.

public function getUploadURL():void {   
    var request:URLRequest = new URLRequest();
    request.url = getPath();
    request.method = URLRequestMethod.GET;

    _loader = new URLLoader();
    _loader.dataFormat = URLLoaderDataFormat.TEXT;
    _loader.addEventListener(Event.COMPLETE, getBaseURL);

    _loader.load(request);
}

private function getBaseURL(event:Event):void {
    _loader.removeEventListener(Event.COMPLETE, getBaseURL);
}

The issue is that my getBaseURL gets executed automatically after I have executed the code at least once, but that is the case only in IE. What happens is I call my getUploadURL, I make sure the server sends an event that will result in an Event.COMPLETE, so the getBaseURL gets executed, and the listener is removed. If I call the getUploadURL method and put the wrong path, I do not get an Event.COMPLETE but some other event, and getBaseURL should not be executed.

That is the correct behavior in FireFox. In IE, it looks like the load() method does not actually call the server, it jumps directly to the getBaseURL() for the Event.COMPLETE. I checked the willTrigger() and hasEventListener() on _loader before assigning the new URLLoader, and it turns out the event has been well removed.

I hope I make sense, I simplified my code. To sum up quickly: in FireFox it works well, but in IE, the first call will work but the second call won't really call the .load() method; it seems it uses the previously stored result from the first call.

I hope someone can please help me, Thank you,

Rudy

A: 

Possibly the request has been cached.

var hdr:URLRequestHeader = new URLRequestHeader("pragma", "no-cache");
....
request.requestHeaders.push(hdr);
Sander Pham
Thanks, I just tried but unfortunately it has made no difference.
Rudy
A: 

Try adding a random variable to the url to prevent caching.

var url:String = getPath();
//if path already contains some variables, replace ? with &
url += "?random=" + Math.random(); 
request.url = getPath();
Amarghosh
Your solution fixed my issue! Thanks.So this has to do with caching. But what happens if the Math.random() gives the same result? Then it will use the cached URLRequest no?!Is there any way I can force it to not cache. The solution using URLRequestHeader did not work unfortunately.Thanks a lot though.
Rudy
@Rudy the chances of `Math.random` giving same results within same session (before cache is cleared), is very small; but if you really want to make sure, you can use the current time - the number of milliseconds passed since linux epoch. `url += "?random=" + (new Date()).getTime();`
Amarghosh
Thank you, I will use the getTime() function, that's a very good idea. Thanks a lot!
Rudy