MockXMLHttpRequest 类文档

本文档提供了 MockXMLHttpRequest 类的 JavaScript 接口。该类模拟了浏览器中 XMLHttpRequest 的功能,用于发起 HTTP 请求,支持设置请求方法、URL、请求头以及发送数据,并处理响应。

类描述

MockXMLHttpRequest 是一个模拟 HTTP 请求的工具类,支持 GET、POST 等请求方法,能够设置请求头、发送数据(字符串或对象),并获取响应状态、状态文本和响应内容。异步执行当前暂不支持,请求以同步方式处理。

方法

open(method, url)

初始化 HTTP 请求。

参数

  • method (string): HTTP 请求方法(如 "GET"、"POST")。
  • url (string): 请求的目标 URL。

行为

  • 设置请求方法和 URL。

示例

req.open("GET", "https://api.example.com/data");

open(method, url, async)

初始化 HTTP 请求,带异步标志(当前异步暂不支持)。

参数

  • method (string): HTTP 请求方法。
  • url (string): 请求的目标 URL。
  • async (boolean): 是否异步执行(当前忽略,固定为同步)。

行为

  • 设置请求方法、URL 和异步标志。

示例

req.open("POST", "https://api.example.com/submit", false);

setRequestHeader(header, value)

设置单个请求头。

参数

  • header (string): 请求头名称(如 "Content-Type")。
  • value (string): 请求头值(如 "application/json")。

行为

  • 仅在 req.open 后调用有效,将请求头添加到内部存储。

示例

req.open("POST", "https://api.example.com/submit");
req.setRequestHeader("Content-Type", "application/json");

setRequestHeader(headers)

批量设置请求头。

参数

  • headers (Object): 键值对对象,键为请求头名称,值为请求头值。

行为

  • 仅在 req.open 后调用有效,将所有请求头添加到内部存储。

示例

req.open("POST", "https://api.example.com/submit");
req.setRequestHeader({
  "Content-Type": "application/json",
  "Authorization": "Bearer token123"
});

send()

发送 HTTP 请求(无请求体)。

行为

  • 如果未调用 req.open ,抛出错误:"Invalid state: the request has not been opened."。
  • 使用设置的 method、URL 和请求头发起 HTTP 请求。
  • 更新 status(HTTP 状态码)、statusText(状态文本)、responseText(响应内容),并将 readyState 设置为 DONE(4)。
  • 如果请求失败,抛出错误:"Error sending request: ..."。

示例

req.open("GET", "https://api.example.com/data");
req.send();
console.log(req.responseText); // 输出响应内容

send(data)

发送 HTTP 请求(带请求体)。

参数

  • data (any): 请求体数据,可以是字符串或对象。
  • 如果是字符串,直接发送。
  • 如果是对象,使用 JSON 序列化后发送。

行为

  • 如果未调用 req.open ,抛出错误:"Invalid state: the request has not been opened."。
  • 设置 Content-Type(如果未设置)并发送请求体。
  • 更新 status、statusText、responseText,并将 readyState 设置为 DONE(4)。
  • 如果请求失败,抛出错误:"Error sending request: ..."。

示例

req.open("POST", "https://api.example.com/submit");
req.setRequestHeader("Content-Type", "application/json");
req.send({ key: "value" });
console.log(req.responseText); // 输出响应内容

getReadyState()

获取当前请求状态。

返回值

  • number: 当前的 readyState 值(0-4)。

示例

req.open("GET", "https://api.example.com/data");
console.log(req.getReadyState()); // 输出: 1

getResponseText()

获取响应内容。

返回值

  • string | null: 服务器返回的响应内容,请求未完成时为 null。

示例

req.open("GET", "https://api.example.com/data");
req.send();
console.log(req.getResponseText()); // 输出响应内容

getStatus()

获取 HTTP 状态码。

返回值

  • number: HTTP 状态码(如 200、404),初始为 0。

示例

req.open("GET", "https://api.example.com/data");
req.send();
console.log(req.getStatus()); // 输出: 200(成功时)

getStatusText()

获取 HTTP 状态文本。

返回值

  • string | null: HTTP 状态文本(如 "OK"、"Not Found"),初始为 null。

示例

req.open("GET", "https://api.example.com/data");
req.send();
console.log(req.getStatusText()); // 输出: "OK"(成功时)

注意事项

  • 异步支持:当前版本不支持异步请求(async 参数被忽略,请求以同步方式执行)。
  • 请求头设置:必须在调用 open 后、send 前设置请求头,否则无效。
  • 数据处理:非字符串类型的 send(data) 参数会自动 JSON 序列化。
  • 异常处理:
  • 如果请求未打开,send 方法抛出 "Invalid state: the request has not been opened."。
  • 如果请求失败(网络错误、URL 无效等),抛出 "Error sending request: ..."。
  • 响应处理:响应内容以字符串形式存储在 responseText 中,需自行解析(如 JSON 格式)。
作者:北京-产品-yantings  创建时间:2026-07-01 09:56
最后编辑:北京-产品-yantings  更新时间:2026-07-07 18:14
上一篇:
下一篇: