Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

xhr

Package Overview
Dependencies
Maintainers
1
Versions
60
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xhr

small xhr abstraction

  • 1.6.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.4M
decreased by-8.44%
Maintainers
1
Weekly downloads
 
Created

What is xhr?

The xhr npm package is a simple and lightweight library for making XMLHttpRequests in a browser environment. It provides a straightforward API for performing HTTP requests, handling responses, and managing errors.

What are xhr's main functionalities?

Basic GET Request

This feature allows you to perform a basic GET request to a specified URL. The callback function handles the response or any errors that occur.

const xhr = require('xhr');
xhr({ url: 'https://api.example.com/data' }, function (err, resp, body) {
  if (err) {
    console.error(err);
  } else {
    console.log(body);
  }
});

POST Request with Data

This feature allows you to perform a POST request with a JSON payload. The request includes headers to specify the content type, and the callback function handles the response or any errors.

const xhr = require('xhr');
xhr({
  url: 'https://api.example.com/data',
  method: 'POST',
  body: JSON.stringify({ key: 'value' }),
  headers: { 'Content-Type': 'application/json' }
}, function (err, resp, body) {
  if (err) {
    console.error(err);
  } else {
    console.log(body);
  }
});

Handling Response Status Codes

This feature demonstrates how to handle different HTTP response status codes. The callback function checks the status code and logs the appropriate message.

const xhr = require('xhr');
xhr({ url: 'https://api.example.com/data' }, function (err, resp, body) {
  if (err) {
    console.error(err);
  } else if (resp.statusCode === 200) {
    console.log('Success:', body);
  } else {
    console.log('Error:', resp.statusCode);
  }
});

Other packages similar to xhr

Keywords

FAQs

Package last updated on 05 May 2014

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