蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 AJAX 局部刷新载荷对比、XHR 状态机、XML 解析与增量搜索建议离线验证

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-ajax-intro.py
explore-ajax-intro.py133 行
"""Offline Classic ASP AJAX Introduction verification lab (IIS ASP & browser absent)."""
import json
import xml.etree.ElementTree as ET


class MockXMLHttpRequest:
    """Models standard XMLHttpRequest readyState lifecycle (0..4) and status=200 dispatch."""

    def __init__(self) -> None:
        self.readyState = 0
        self.status = 0
        self.responseText = ""
        self.state_log: list[int] = [0]
        self.dom_updates: list[str] = []

    def _notify(self) -> None:
        self.state_log.append(self.readyState)
        if self.readyState == 4 and self.status == 200:
            self.dom_updates.append(self.responseText)

    def open(self, method: str, url: str, async_flag: bool = True) -> None:
        self.method = method
        self.url = url
        self.async_flag = async_flag
        self.readyState = 1
        self._notify()

    def send(self, server_body: str, status: int = 200) -> None:
        self.readyState = 2
        self._notify()
        self.readyState = 3
        self._notify()
        self.status = status
        self.responseText = server_body
        self.readyState = 4
        self._notify()


def main() -> None:
    cases = []

    # Case 1: Partial AJAX fragment vs full page reload payload
    full_page_html = (
        "<!DOCTYPE html><html><head><title>Search</title></head>"
        "<body><header>Navigation Bar</header><main><input id='q'>"
        "<div id='suggestions'>AJAX, ASP, ADO</div></main><footer>Copyright</footer></body></html>"
    )
    partial_fragment = "AJAX, ASP, ADO"
    dom_state = {"header": "Navigation Bar", "suggestions": ""}
    dom_state["suggestions"] = partial_fragment
    expected1 = {
        "partial_smaller_than_full": True,
        "header_preserved": "Navigation Bar",
        "updated_fragment": "AJAX, ASP, ADO",
    }
    actual1 = {
        "partial_smaller_than_full": len(partial_fragment.encode("utf-8")) < len(full_page_html.encode("utf-8")),
        "header_preserved": dom_state["header"],
        "updated_fragment": dom_state["suggestions"],
    }
    cases.append({
        "name": "ajax_partial_update_vs_full_reload",
        "summary": "Verify AJAX transfers only the small data fragment and updates DOM without reloading page.",
        "expected": expected1,
        "actual": actual1,
        "passed": actual1 == expected1,
    })

    # Case 2: XMLHttpRequest readyState progression 0->1->2->3->4
    xhr = MockXMLHttpRequest()
    xhr.open("GET", "gethint.asp?q=a", True)
    xhr.send("Anna , Amanda", status=200)
    expected2 = {
        "states": [0, 1, 2, 3, 4],
        "dom_update_count": 1,
        "final_dom_text": "Anna , Amanda",
    }
    actual2 = {
        "states": xhr.state_log,
        "dom_update_count": len(xhr.dom_updates),
        "final_dom_text": xhr.dom_updates[-1],
    }
    cases.append({
        "name": "ajax_xhr_lifecycle_states",
        "summary": "Verify XMLHttpRequest progresses through readyState 0..4 and updates DOM only on 4+200.",
        "expected": expected2,
        "actual": actual2,
        "passed": actual2 == expected2,
    })

    # Case 3: Four Internet Standards stack (XHR + JS/DOM + CSS + XML)
    xml_payload = (
        "<suggestions>"
        "<item>AJAX</item>"
        "<item>ASP</item>"
        "<item>ADO</item>"
        "</suggestions>"
    )
    root = ET.fromstring(xml_payload)
    items = [el.text for el in root.findall("item") if el.text]
    expected3 = ["AJAX", "ASP", "ADO"]
    cases.append({
        "name": "ajax_xml_payload_parsing",
        "summary": "Verify XML response parsing into DOM suggestion items.",
        "expected": expected3,
        "actual": items,
        "passed": items == expected3,
    })

    # Case 4: Google Suggest incremental prefix filtering
    dictionary = ["ajax", "ajax asp", "ajax database", "asp", "ado"]
    step1 = [w for w in dictionary if w.startswith("a")]
    step2 = [w for w in dictionary if w.startswith("aj")]
    step3 = [w for w in dictionary if w.startswith("ajax d")]
    expected4 = {"len_a": 5, "len_aj": 3, "match_ajax_d": ["ajax database"]}
    actual4 = {"len_a": len(step1), "len_aj": len(step2), "match_ajax_d": step3}
    cases.append({
        "name": "ajax_google_suggest_prefix_narrowing",
        "summary": "Verify incremental keystroke prefix queries monotonically narrow suggestions.",
        "expected": expected4,
        "actual": actual4,
        "passed": actual4 == expected4,
    })

    print(json.dumps({
        "version": "CPython 3.12.14 offline Classic ASP / VBScript verifier (IIS ASP absent)",
        "cases": cases,
    }, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/asp-ajax-intro-lab/explore-ajax-intro.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-ajax-intro.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:ASP

课程关联工程包(登记关联36章) · 含当前章节或实例 · mbskycom202612a1425.zip

下载课程关联工程包:ASP

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1423.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习