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

req-ajax

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

req-ajax

Standalone library for ajax requests

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-80%
Maintainers
1
Weekly downloads
 
Created
Source

req-ajax

Standalone library for ajax requests.

Installation

Execute this command in your project for install the package:

npm install --save req-ajax

After the download you should install the dependencies and build es2015 version with babel.js, so execute this:

cd node_modules/req-ajax; npm run build; cd -

The file ajax.js and ajax.min.js are in dist directory.

Usage

Simple external script in your html file:

<script src="node_modules/req-ajax/dist/ajax.min.js"></script>

API

All methods return a Promise object.

Methods

ajax.query(Object)

the Object is the config of that request, this object can have 3 properties:

  1. url {String} is required.
  2. method {String} is required.
  3. params {Object} is optional, this property is for specific queries. The queries like this foo=bar&bar=foo can be transformed to { foo: 'bar', bar: 'foo' } in the JavaScript object notation, so is much better for make queries and easy.

Example:

ajax.query({
    url: 'somepath',
    method: 'GET',
    params: {
        foo: 'bar'
    }
    // the url should be 'somepath?foo=bar'.
}).then(function (res) {
    // Do something with the response...
}).catch(function (err) {
    // Manage the error of the request (if appear).
})

ajax.get(url [, json])

This method is an shorthand for GET requests.

Params:

  1. url {String} is required.
  2. json {Boolean} is optional, default false. This param is for when the response is json string, if json is true the response is parser for convert it to a object normal.

Per Example the file somefile.json contain { "foo": "bar" }:

ajax.get('somefile.json', true)
    .then(function (res) {
        if (res.foo === 'bar') {
            // Do something...
        }
    }).catch(function (err) {
        // Trap the error.
    })

ajax.post(url, data [, json])

This method is an shorthand for POST requests.

Params:

  1. url {String} is required.
  2. data {String|Number|Object|Boolean} is required.
  3. json {Boolean} is optional, default false. This param is for when the data is object normal and will send it as a json string.

Example:

const data = { foo: 'bar' }

// The data is transformed to '{ "foo": "bar"}'
ajax.post('somepath', data, true)
    .then(function ()
        // The post request was complete.
    }).catch(function (err) {
        // Something it's wrong.
    })

For more examples and test execute npm run test and open your browser in the url http://localhost:8080.

Keywords

FAQs

Package last updated on 10 Mar 2017

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