views:

57

answers:

2

I want it to do another action if the ID I key in does not exist or have an error, how do I trace the error event?

loadID=searchArea.text;
myLoader2.load(new URLRequest("GetData.aspx?id="+loadID));

if (errorEvent) {
trace("Please key in the correct ID");
} else {
myLoader2.addEventListener(Event.COMPLETE,processXML2);
}
+1  A: 

See this page.

excerpt:

public function URLRequestExample() {
    var loader:URLLoader = new URLLoader();
    configureListeners(loader);

    var request:URLRequest = new URLRequest("XMLFile.xml");
    try {
        loader.load(request);
    } catch (error:Error) {
        trace("Unable to load requested document.");
    }
}

private function configureListeners(dispatcher:IEventDispatcher):void {
    dispatcher.addEventListener(Event.COMPLETE, completeHandler);
    dispatcher.addEventListener(Event.OPEN, openHandler);
    dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
    dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
    dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
    dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}

private function completeHandler(event:Event):void {
    var loader:URLLoader = URLLoader(event.target);
    trace("completeHandler: " + loader.data);
}

private function openHandler(event:Event):void {
    trace("openHandler: " + event);
}

private function progressHandler(event:ProgressEvent):void {
    trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
}

private function securityErrorHandler(event:SecurityErrorEvent):void {
    trace("securityErrorHandler: " + event);
}

private function httpStatusHandler(event:HTTPStatusEvent):void {
    trace("httpStatusHandler: " + event);
}

private function ioErrorHandler(event:IOErrorEvent):void {
    trace("ioErrorHandler: " + event);
}
Gabriel McAdams
+3  A: 

You would need to add an event listener for the error event as well. something like:

loadID=searchArea.text;

myLoader2.addEventListener(Event.COMPLETE,processXML2);
myLoader2.addEventListener(IOErrorEvent.IO_ERROR,onIOError);

myLoader2.load(new URLRequest("GetData.aspx?id="+loadID));

then create a method to hanel the IO error event.

public function onIOError(e:IOErrorEvent):void
{
    // do error handling in here
    trace("Please key in the correct ID");
}
Chris Gutierrez
if i have runAction(event.target) after the line of processXML2.i need to move it into the function processXML2, but whats the correct target here for runAction(?)
Hwang