更新:2007 年 11 月
注册 Web 请求的完成请求事件的处理程序。
MyWebRequest.add_completed(handler);
参数
项 |
定义 |
|---|---|
handler |
注册来处理完成请求事件的函数。 |
备注
与 Web 请求关联的执行器通过调用完成方法来引发完成请求事件。当关联的执行器所执行的工作完成时,将调用处理程序函数来处理所返回的数据。请注意,完成并不表示成功。执行器完成其工作时的状态为:完成、中止或超时。
注册的事件处理程序函数必须接受两个参数:
对发出网络请求的执行器的引用。通过访问执行器,可以检查其状态和检索响应数据。
eventArgs 参数,它由引发完成请求事件的执行器设置。对于默认执行器,此参数设置为 Sys.EventArgs.empty 属性。
事件处理程序可使用以下某个执行器属性来确定执行器的状态:responseAvailable、aborted 或 timedOut。
仅当 responseAvailable 返回 true 时,事件处理程序才能访问执行器的其他响应信息。
示例
下面的示例演示如何添加和移除完成事件处理程序。此代码摘自 WebRequest 类概述中的一个完整示例。
// This function adds and removes the
// Web request completed event handler.
function WebRequestCompleted()
{
// Instantiate the WebRequest.
var wRequest = new Sys.Net.WebRequest();
// Set the request Url.
wRequest.set_url(getPage);
// Set the web request completed event handler,
// for processing return data.
wRequest.add_completed(OnWebRequestCompleted);
alert("Added Web request completed handler");
// Remove the web request completed event handler.
// Comment the following two lines if you want to
// use the handler.
wRequest.remove_completed(OnWebRequestCompleted);
alert("Removed handler; the Web request return is not processed.");
// Execute the request.
wRequest.invoke();
}