更新:2007 年 11 月
利用浏览器对 XMLHTTP 的支持,进行异步网络请求。
命名空间:Sys.Net
**继承:**无
var executor = new Sys.Net.XMLHttpExecutor();
成员
名称 |
说明 |
|---|---|
在派生类中实现时,初始化 Sys.Net.XMLHttpExecutor 类的新实例。 |
|
停止由执行器发出的、挂起的网络请求。 |
|
执行在相关联的 WebRequest 实例中指定的网络请求。 |
|
返回响应头。 |
|
获取指定响应头的值。 |
|
返回一个值,该值指示执行器是否已中止。 |
|
返回一个值,该值指示网络请求是否在未中止或超时的情况下返回。 |
|
获取响应正文的文本表示形式。 |
|
返回一个值,该值指示执行器是否已将请求转发给浏览器的 XMLHTTP 对象。 |
|
获取浏览器的 XMLHTTP 对象的状态代码。 |
|
获取浏览器的 XMLHTTP 对象的状态文本。 |
|
返回一个值,该值指示执行器是否已超时。 |
|
返回一个 XMLDOM 对象,该对象包含浏览器的 XMLHTTP 对象的 XML 响应。 |
备注
XmlHttpExecutor 类充当默认执行器,而且是 WebRequestExecutor 抽象类的实现。
由于已设置了默认执行器,因此不必创建该类的实例并将其与 Web 请求进行关联。但是,如果定义自定义执行器,则必须创建该执行器的实例,并将其设置为 Web 请求的默认执行器。
在完成网络调用后,XmlHttpExecutor 对象应仅用于获取响应数据,在此之后应丢弃该对象。
说明: |
|---|
有关 XMLHTTP 对象的更多信息,请参见 About Native XMLHTTP(关于本机 XMLHTTP)。 |
示例
下面的示例演示如何使用 XmlHttpExecutor 类方法和属性。该示例演示一个网页以及与 XmlHttpExecutor 类进行交互的客户端脚本。
var resultElementId;
function pageLoad()
{
resultElementId = $get("ResultId");
}
// This function aborts a Web request.
function AbortWebRequest()
{
// Create the WebRequest object.
wRequest = new Sys.Net.WebRequest();
// Set the request Url.
wRequest.set_url("getTarget.htm");
// Clear the results area.
resultElementId.innerHTML = "";
// Set the Completed event handler,
// for processing return data
wRequest.add_completed(OnCompleted);
// Make the request.
wRequest.invoke();
// Get the current executor.
var executor = wRequest.get_executor();
// Abort the request.
executor.abort();
// Check if the executor is aborted.
var execAborted =
executor.get_aborted();
alert("Executor aborted: " + execAborted);
}
// This function executes a Web request.
function ExecuteWebRequest()
{
// Create the WebRequest object.
wRequest = new Sys.Net.WebRequest();
// Set the request Url.
wRequest.set_url("getTarget.htm");
// Set the Completed event handler, for processing return data
wRequest.add_completed(OnCompleted);
// Clear the results area.
resultElementId.innerHTML = "";
// To use executeRequest you must instantiate the
// executor, assign it to the Web request instance,
// then call the executeRequest function.
// Note: Normally to make a Web request you use
// the invoke method of the WebRequest instance.
var executor = new Sys.Net.XMLHttpExecutor();
wRequest.set_executor(executor);
executor.executeRequest();
var started = executor.get_started();
alert("Executor started: " + started);
}
// This is the event handler called after
// the Web request returns.
function OnCompleted(executor, eventArgs)
{
if(executor.get_responseAvailable())
{
// Get the Web request instance.
var webReq = executor.get_webRequest();
// Display request Url.
alert(webReq.get_url());
// Clear the previous results.
resultElementId.innerHTML = "";
// Display the Web request status.
resultElementId.innerHTML +=
"Request Status: [" + executor.get_statusCode() + " " +
executor.get_statusText() + "]" + "<br/>";
// Display the Web request headers.
resultElementId.innerHTML += "Headers: <br/>";
// Get all the headers.
resultElementId.innerHTML +=
"All Request Headers: " +
executor.getAllResponseHeaders() + "<br/>";
// Get a specific header.
resultElementId.innerHTML +=
"Content-Type Header: " +
executor.getResponseHeader("Content-Type") +
"<br/>";
// Display Web request body.
resultElementId.innerHTML += "Body: <br/>";
resultElementId.innerText +=
executor.get_responseData();
}
else
{
if (executor.get_timedOut())
alert("Timed Out");
else
if (executor.get_aborted())
alert("Aborted");
}
}
// This is the event handler called after
// the Web request returns. It is designed
// for Web requests that return XML.
function OnSucceededXml(executor, eventArgs)
{
if (executor.get_responseAvailable())
{
// Display XML.
if (document.all)
resultElementId.innerText += executor.get_xml().xml;
else
// Firefox
resultElementId.textContent += "First node: " +
executor.get_xml().documentElement.nodeName;
}
else
{
if (executor.get_timedOut())
alert("Timed Out");
else
if (executor.get_aborted())
alert("Aborted");
}
}
// This function executes a Web request
// to get XML data.
function GetXml()
{
// Create the WebRequest object.
wRequest = new Sys.Net.WebRequest();
// Set the request Url.
wRequest.set_url("getTarget.xml");
// Set the Completed event handler
// for processing return data.
wRequest.add_completed(OnSucceededXml);
// Clear the results area.
resultElementId.innerText = "";
// Invoke the Web request.
wRequest.invoke();
}
// This function aborts a Web request.
function AbortWebRequest()
{
// Create the WebRequest object.
wRequest = new Sys.Net.WebRequest();
// Set the request Url.
wRequest.set_url("getTarget.htm");
// Clear the results area.
resultElementId.innerHTML = "";
// Set the Completed event handler,
// for processing return data
wRequest.add_completed(OnCompleted);
// Make the request.
wRequest.invoke();
// Get the current executor.
var executor = wRequest.get_executor();
// Abort the request.
executor.abort();
// Check if the executor is aborted.
var execAborted =
executor.get_aborted();
alert("Executor aborted: " + execAborted);
}
说明: