[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-TW/docs/Web/API/XMLHttpRequest_API/Using_XMLHttpRequest [Back]  [Original]

XMLHttpRequest - Web API | MDN

MDN Web Docs

View in English Always switch to English

XMLHttpRequest

Baseline Widely available *

This feature is well established and works across many devices and browser versions. Its been available across browsers since 20157.

* Some parts of this feature may have varying levels of support.

XMLHttpRequest HTTP

XMLHttpRequest

HTTP

  1. XMLHttpRequest
  2. URL

XMLHttpRequest HTTP

js
function reqListener() {
  console.log(this.responseText);
}

const req = new XMLHttpRequest();
req.addEventListener("load", reqListener);
req.open("GET", "http://www.example.org/example.txt");
req.send();

In this article

XMLHttpRequest XMLHttpRequest.open() async true XMLHttpRequest Web Worker

XMLHttpRequest XML **XML** XML

XMLHttpRequest() XMLHttpRequest

responseXML

XMLHttpRequest XML responseXML XML DOM XML

  1. XPath
  2. XML
  3. XMLSerializer DOM
  4. XML RegExp RegExp XML

XMLHttpRequest responseXML HTML XMLHttpRequest HTML

HTML responseText

XMLHttpRequest HTML responseText HTML HTML

  1. XMLHttpRequest.responseXML XMLHttpRequest HTML
  2. fragment.body.innerHTML DOM
  3. HTML responseText RegExp RegExp HTML

XMLHttpRequest XMLHttpRequest XMLHttpRequest overrideMimeType()

js
const req = new XMLHttpRequest();
req.open("GET", url);
// 
req.overrideMimeType("text/plain; charset=x-user-defined");
/*  */

responseType

"arraybuffer" responseType ArrayBuffer

js
const req = new XMLHttpRequest();

req.onload = (e) => {
  const arraybuffer = req.response; //  responseText
  /*  */
};
req.open("GET", url);
req.responseType = "arraybuffer";
req.send();

XMLHttpRequest

XMLHttpRequest DOM progress ProgressEvent

progress

load

response

js
const req = new XMLHttpRequest();

req.addEventListener("progress", updateProgress);
req.addEventListener("load", transferComplete);
req.addEventListener("error", transferFailed);
req.addEventListener("abort", transferCanceled);

req.open();

// 

// 
function updateProgress(event) {
  if (event.lengthComputable) {
    const percentComplete = (event.loaded / event.total) * 100;
    // 
  } else {
    // 
  }
}

function transferComplete(evt) {
  console.log("");
}

function transferFailed(evt) {
  console.log("");
}

function transferCanceled(evt) {
  console.log("");
}

XMLHttpRequest

open() progress

updateProgress() total loaded lengthComputable false

XMLHttpRequest XMLHttpRequest.upload

js
const req = new XMLHttpRequest();

req.upload.addEventListener("progress", updateProgress);
req.upload.addEventListener("load", transferComplete);
req.upload.addEventListener("error", transferFailed);
req.upload.addEventListener("abort", transferCanceled);

req.open();

file:

load progress

loadend abortload error

js
req.addEventListener("loadend", loadEnd);

function loadEnd(e) {
  console.log("");
}

loadend

js
function getHeaderTime() {
  console.log(this.getResponseHeader("Last-Modified")); //  GMTString  null
}

const req = new XMLHttpRequest();
req.open(
  "HEAD", //  HEAD
  "your-page.html",
);
req.onload = getHeaderTime;
req.send();

js
function getHeaderTime() {
  const lastVisit = parseFloat(
    window.localStorage.getItem(`lm_${this.filepath}`),
  );
  const lastModified = Date.parse(this.getResponseHeader("Last-Modified"));

  if (isNaN(lastVisit) || lastModified > lastVisit) {
    window.localStorage.setItem(`lm_${this.filepath}`, Date.now());
    isFinite(lastVisit) && this.callback(lastModified, lastVisit);
  }
}

function ifHasChanged(URL, callback) {
  const req = new XMLHttpRequest();
  req.open("HEAD" /*  HEAD -  */, URL);
  req.callback = callback;
  req.filepath = URL;
  req.onload = getHeaderTime;
  req.send();
}

js
//  "your-page.html"
ifHasChanged("your-page.html", function (modified, visit) {
  console.log(
    ` '${this.filepath}'  ${new Date(
      modified,
    ).toLocaleString()} `,
  );
});

document.lastModified

XMLHttpRequest

CORS Web XMLHttpRequest INVALID_ACCESS_ERR

URL ?&

http://example.com/bar.html -> http://example.com/bar.html?12345
http://example.com/bar.html?foobar=baz -> http://example.com/bar.html?foobar=baz&12345

URL

URL

js
const req = new XMLHttpRequest();

req.open("GET", url + (/\?/.test(url) ? "&" : "?") + new Date().getTime());
req.send(null);

XMLHttpRequest Access-Control-Allow-Origin HTTP

XMLHttpRequest

XMLHttpRequest status=0 statusText=null UNSENT XMLHttpRequest open() XMLHttpRequest XMLHttpRequest XMLHttpRequest onunload open() DOMActivate unload

Specification
XMLHttpRequest
# interface-xmlhttprequest


Web Proxy Viewer  |  New URL  |  Original Page