如果URLRequest返回错误,或者不存在

时间:2010-01-08 05:20:15

标签: actionscript-3

如果我输入的ID不存在或有错误,我希望它执行另一个操作,如何跟踪错误事件?

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

2 个答案:

答案 0 :(得分: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");
}

答案 1 :(得分: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);
}