DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解
XMLHTTP 異步取值的超時問題測試記錄
編輯:AJAX詳解     

本文記錄了一組用於測試 XMLHTTP 異步取得回傳值時「代碼超時」現象的示例,包含客戶端與服務端兩部分程式碼。客戶端檔案請另存為 test_2.htm:

<Html>
<body>
XMLhttp 異步的例子:
URL: <input name=a type=textbox value="test_s.ASP" style="width:600px">
<input onclick="getXML()" type="button" value="得到源代碼">
<input name=geti type=textbox value="10000">
<input onclick="if(xh && xh.responseText){alert(xh.responseText);oDiv.innerHtml=xh.responseText}" type="button" value="顯示源代碼">
<div id=m style="display:none"></div>

<script language=Javascript>
var oDiv
var xh
function getXML()
{
    oDiv = document.all.m;
    oDiv.innerHtml = "正在裝載欄目數據,請稍侯.......";
    oDiv.style.display = "";
    xh = new ActiveXObject("Microsoft.XMLHTTP")
    xh.onreadystatechange = getReady;
    xh.open("POST", a.value + "?geti=" + geti.value, true);
    xh.send();
}

function getReady()
{
    if(xh.readyState == 4)
    {
        if(xh.status == 200)
        {
            oDiv.innerHtml = xh.responseText;
            oDiv.innerHTML = oDiv.innerHtml + "load over"
        }
        else
        {
            oDiv.innerHtml = "抱歉,裝載數據失敗。原因:" + xh.statusText
            // 測試發現:運行時間過長會出現 Internal Server Error,
            // 但具體超時時間是多少、在哪裡設定,仍待確認。
        }
    }
    else
    {
        oDiv.innerHtml = "已經有數據了,不過 readyState 檢測未通過<br>";
        switch (xh.readyState) {
            case 0: // 未初始化,尚未調用 open
            case 1: // 已建立,尚未調用 send
            case 2: // send 已調用,狀態與 http 頭未知
            case 3: // 數據傳送中,此時取 responseText 可能出錯
            case 4: // 完成,可取得完整回應
            default: // 未知狀態
        }
        oDiv.innerHTML = oDiv.innerHtml + "。請等待"
    }
}
</script>
</body>
</Html>

服務端檔案請另存為 test_s.ASP,透過 geti 參數控制迴圈次數,藉此製造不同長度的處理時間,用來觀察異步請求在何種耗時下會觸發超時:

<%
dim i
dim r
dim t
dim lngGetI

lngGetI = request.querystring("geti")

on error resume next
lngGetI = clng(lngGetI)
if err <> 0 then
    response.write "GetError: not isnumber()"
    response.end()
end if

if lngGetI < 0 or lngGetI > 500000 then
    lngGetI = 10000
end if

response.write "Run Repeat At:" & lngGetI & "<br>"

t = timer
for i = 1 to lngGetI
    r = r & i
    if i mod 20 = 0 then
        r = r & "a<br>"
    end if
next

response.write "run timer:" & cdbl(timer() - t) & "<br>"
response.write r
%>

透過調整 geti 的數值,即可重現不同處理耗時下的行為,進而定位「Internal Server Error」究竟發生在何種時間門檻,以及該門檻可在何處設定。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved