npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nanoajax

v0.4.3

Published

A small ajax lib

Downloads

3,504

Readme

nanoajax

An ajax library you need a microscope to see.

Weighs in at 620 bytes gzipped and minified. It is very basic, but contains support for cross-domain requests back to somewhat older browsers (See Compatibility).

API Changes

  • Passing a string url instead of a params object has been removed in v0.4.0
  • The params object was introduced in v0.2.1, and is the only way to use POST requests.
  • Passing a FormData object as the body will cause the Content Type header to not be set, as appropriate.

Install

You can use npm or bower:

npm install --save nanoajax
bower install --save nanoajax

Can be used via browserify or webpack:

var nanoajax = require('nanoajax')

Or you can use the global script:

<script src="/nanoajax.min.js"></script>

(You can build that script with: npm install -g uglify-js && ./make)

Use

GET

nanoajax.ajax({url:'/some-get-url'}, function (code, responseText) { ... })

POST

nanoajax.ajax({url: '/some-post-url', method: 'POST', body: 'post=content&args=yaknow'}, function (code, responseText, request) {
    # code is response code
    # responseText is response body as a string
    # request is the xmlhttprequest, which has `getResponseHeader(header)` function
})

Documentation

var xhrRequest = nanoajax.ajax(params, callback)

Simple and small ajax function. Takes a parameters object and a callback function.

Parameters:

  • url: string, required
  • headers: object of {header_name: header_value, ...}
  • body:
    • string (sets content type to 'application/x-www-form-urlencoded' if not set in headers)
    • FormData (doesn't set content type so that browser will set as appropriate)
  • method: 'GET', 'POST', etc. Defaults to 'GET' or 'POST' based on body
  • cors: If your using cross-origin, you will need this true for IE8-9 (to use the XDomainRequest object, also see Compatibility)

The following parameters are passed directly onto the request object:

IMPORTANT NOTE: The caller is responsible for compatibility checking.

  • responseType: string, various compatability, see xhr docs for enum options
  • withCredentials: boolean, IE10+, CORS only
  • timeout: long, ms timeout, IE8+
  • onprogress: callback, IE10+

Callback function prototype:

  • statusCode: integer status or null
    • if request errors for some browsers (notably Chrome), this is 0 (and response is "Error")
  • response:
    • if responseType set and supported by browser, this is an object of some type (see docs)
    • otherwise if request completed, this is the string text of the response
    • if request is aborted, this is "Abort"
    • if request times out, this is "Timeout"
    • if request errors before completing (probably a CORS issue), this is "Error"
  • request object

Returns the request object. So you can call .abort() or other methods

Compatibility

nanoajax works on android, iOS, IE8+, and all modern browsers, with some (known) caveats.

  • Safari is conservative with cookies and will not allow cross-domain cookies to be set from domains that have never been visited by the user.
  • IE8 and IE9 do not support cookies in cross-domain requests in this library. There are other solutions out there, but this library has chosen small over edge case compatibility.

License

MIT found in LICENSE file.