我通过以下代码使用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 !
...
这样用户将等待15秒以获得AJAX,然后看到如下结果:
Connecting...Reading...
无论如何都要独立显示两个回答!?
例如,某种方式我们得到了下面描述的结果:
用户首先看到文字正在连接... ,然后在10秒后(连接多头的时间!)文字发生变化,用户看到文字正在阅读...... ?