2010-01-08 20 views
1

如果ID鍵入的鍵不存在或有錯誤,我希望它執行另一個操作,那麼如何跟蹤錯誤事件?如果URLRequest返回錯誤或不存在

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); 
} 

回答

4

您將需要添加事件偵聽器的錯誤事件也是如此。如:

loadID=searchArea.text; 

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

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

然後創建一個方法來處理IO錯誤事件。

public function onIOError(e:IOErrorEvent):void 
{ 
    // do error handling in here 
    trace("Please key in the correct ID"); 
} 
+0

如果我有processXML2行後面的runAction(event.target)。 我需要將它移動到函數processXML2中,但是runAction(?)的正確目標是什麼? – Hwang 2010-01-08 07:52:05

1

請參閱this page

摘錄:

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); 
}