我通过下面的代码使用 AJAX:
Javascript:
function CallAJAX() {
var url = "MyAJAX.ashx";
xmlRequest.open("POST", url);
xmlRequest.onreadystatechange = function () { ApplyUpdateCallAJAX() }
xmlRequest.send(null);
}
function ApplyUpdateCallAJAX() {
if (xmlRequest.readyState == 4) {
if (xmlRequest.status == 200) {
var response = xmlRequest.responseText;
document.getElementById("resultDIV").innerHTML = response;
}
}
}
.ashx 文件(在 asp.net 中):
...
...
HttpResponse response = context.Response;
response.ContentType = "text/html";
...
response.Write("Connecting...");
// connecting to database - for example takes 10 seconds !
...
response.Write("Reading...");
// reading data from database - for example takes 5 seconds !
...
这样用户将等待 AJAX 15 秒,然后看到如下结果:
Connecting...Reading...
反正有没有独立显示这两个响应!?
例如,我们得到如下结果的某种方式:
用户首先看到文本正在连接...,然后在 10 秒后(连接的时间很长!)文本更改并且用户看到文本正在阅读...?