XMLHttpRequest()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

XMLHttpRequest() 建構式會建立一個新的 XMLHttpRequest 物件。

關於如何使用 XMLHttpRequest 物件的細節,請參照:使用 XMLHttpRequest

語法

js
const request = new XMLHttpRequest();

參數

無。

回傳值

將回傳一個新的 XMLHttpRequest 物件。XMLHttpRequest 物件在呼叫send() 送出要求到伺服器之前,至少要藉著呼叫 open() 來準備好必需的設定。

非標準的 Firefox 語法

Firefox 16 added a non-standard parameter to the constructor that can enable anonymous mode (see Firefox bug 692677). Setting the mozAnon flag to true effectively resembles the AnonXMLHttpRequest() constructor described in older versions of the XMLHttpRequest specification.

js
const request = new XMLHttpRequest(paramsDictionary);

Parameters (non-standard)

objParameters

There are two flags you can set:

mozAnon

Boolean: Setting this flag to true will cause the browser not to expose the origin and user credentials when fetching resources. Most important, this means that cookies will not be sent unless explicitly added using setRequestHeader.

mozSystem

Boolean: Setting this flag to true allows making cross-site connections without requiring the server to opt-in using CORS. Requires setting mozAnon: true, i.e. this can't be combined with sending cookies or other user credentials. This only works in privileged (reviewed) apps (Firefox bug 692677); it does not work on arbitrary webpages loaded in Firefox

參見