Check out "Do you speak JavaScript?" - my latest video course on advanced JavaScript.
Language APIs, Popular Concepts, Design Patterns, Advanced Techniques In the Browser

A strange IOErrorEvent.IO_ERROR event dispatched

Really simple GET request from a flex based application. However it fails if GET parameters are passed.

var obj:Object = {property: "value"};
var urlString:String = "http://site.com?query=" + JSON.encode(obj);
var urlRequest:URLRequest = new URLRequest(urlString);
urlRequest.method = URLRequestMethod.GET;
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, complete);
urlLoader.addEventListener(ProgressEvent.PROGRESS, progress);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityError);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, IOError);
urlLoader.load(urlRequest);

In the above example *IOError* is called. I firstly checked for *crossdomain.xml* on the server, because sometimes that cause such problems. Unfortunately there was crossdomain policy file, so the problem was somewhere else. I removed *JSON.encode(obj)* from the url and everything was ok. The problem was that the passed json encoded string wasn't urlencoded. Here is the fix:

var urlString:String = "http://site.com?query=" + escape(JSON.encode(obj));
If you enjoy this post, share it on Twitter, Facebook or LinkedIn.