Socket
Socket
Sign inDemoInstall

xmlhttprequest

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xmlhttprequest

XMLHttpRequest for Node


Version published
Weekly downloads
1.3M
increased by8.7%
Maintainers
1
Weekly downloads
 
Created

What is xmlhttprequest?

The xmlhttprequest npm package is a JavaScript library that allows you to perform HTTP client functionality, such as making GET and POST requests to servers. It is designed to mimic the behavior of the native XMLHttpRequest object provided by web browsers, making it useful for server-side applications or testing where the native object is not available.

What are xmlhttprequest's main functionalities?

Performing a GET request

This code sample demonstrates how to perform a simple GET request to retrieve data from a specified URL.

var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4 && xhr.status == 200) {
    console.log(xhr.responseText);
  }
};
xhr.open('GET', 'http://example.com', true);
xhr.send();

Performing a POST request

This code sample shows how to perform a POST request to send JSON data to a server.

var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4 && xhr.status == 200) {
    console.log(xhr.responseText);
  }
};
xhr.open('POST', 'http://example.com', true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({ key: 'value' }));

Setting request headers

This code sample illustrates how to set custom HTTP headers for a request.

var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://example.com', true);
xhr.setRequestHeader('X-Custom-Header', 'value');
xhr.send();

Handling errors

This code sample demonstrates how to handle network errors that may occur during the request.

var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
var xhr = new XMLHttpRequest();
xhr.onerror = function() {
  console.error('Request failed');
};
xhr.open('GET', 'http://example.com', true);
xhr.send();

Other packages similar to xmlhttprequest

FAQs

Package last updated on 01 Nov 2011

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc