Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/xhr/mod.ts

An XMLHttpRequest polyfill for Deno CLI and Deploy 🦕
Very Popular
Go to Latest
import * as xhr from "https://deno.land/x/xhr@0.3.0/mod.ts";

Provides a polyfill for Deno CLI and Deploy for XMLHttpRequest (XHR).

When loaded, it will inject XMLHttpRequest into the global namespace along with a couple other key objects.

While the module exports XMLHttpRequest, most use cases it should just be imported before any other dependencies that require XHR to be present:

import "https://deno.land/x/xhr/mod.ts";
import * as lib from "https://other/dependency/that/needs/xhr/lib.js";

This polyfill has several known/intentional limitations from a browser standard XMLHttpRequest:

  • It does not handle XML, though the name implies it, nor does it handle HTML being treated as a response type "document". This uses the browser's built in parser to parse the XML and HTML into a DOM object.
  • Sync is not supported (passing false to the async argument). Most browsers have deprecated it in the main thread. Since this polyfill works by calling Deno's fetch() it is nearly impossible to generate a sync version, plus it is a really bad idea to block a thread while waiting for a server response. Don't do it, don't use software that requires it.