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

extend-ajax

v2.1.3

Published

an AJAX request library

Downloads

15

Readme

extend-ajax

Build Status NPM version Downloads Coverage Status

中文文档

Features

  • ability to specify request headers
  • ability to get response headers
  • if it isn't jsonp,it can cache response data
  • pre-treat response received
  • support jsonp
  • support IE10+

Install

npm i extend-ajax --save

Examples

send a simple post request

ajax('test/post', 'post').send({
   text: 'hello'
}).then(({data})=> {
  console.log(data);
})

send a post request and specify global options

var ajax = require('extend-ajax');
ajax.config({
   host: 'http://xxx.com/',
   convert: (data) => {
     return JSON.parse(data);
   },
   cacheSize: 2
});
ajax('test/post', 'post', {
     header: {
       'Content-Type': 'json',
       'Accept': 'json'
     }
}).send().then(({data})=> {
      console.log(data);
})

file upload(IE10+)

html:

<input name = "file" type = "file" id = "file">

js:
var ajax = require('extend-ajax');
var files = document.getElementById('file').files;
var fileUpload =  ajax('/upload', 'post', {
  header: {
    'Content-Type': 'formData'
  }
}).send({
  file: files
}).then(() => {
  alert('file is successfully uploaded');
});

jsonp

 var JSONP = ajax('/test/jsonp', 'jsonp', {
     jsonpParam: 'callback',
     jsonpName: 'getDataByJsonp'
 });
 JSONP.on('success', function (data) {
     console.log('get data:' + data + ' with jsonp');
     done();
 });
 JSONP.send();

API

ajax(url[,method,options])

create one ajax object, but it can't send a request immediately, you need use ajax.send() to send a request.

  • url <string> a reequest url

  • method <string> request method。default: 'post'.

  • options <object>

    • isAsync <boolean> default: true
    • cacheSize <number> set size of cache, default: 0
    • cacheExp <number> set the cache expiration time relative to the present, default: 300, unit: s.
    • charset <stirng> set http charset,default: 'utf-8'
    • convert <function> pre-treat response data received, the function have one argument:response data.if set it, it must return a data handled,or when success event happen,we can't get data from res.
    • host <string> host url,default: ''.
    • jsonpName <String> name of the callback functions that handle jsonp response,default: 'jsonpCallback'
    • jsonpParma <String> name of the query string parameter to specify the callback,default: 'callback'
    • header <object> set http header.default: {'Content-Type': 'form', 'Accept': 'json'}
      • Content-Type <string> you can set: 'text', 'json', 'form'(default), 'formData', 'html' and standard content-type value
      • Accept <string> you can set: 'text', 'json', 'html' and standard Accept value
      • ...
    • scope <object> default null, define the scope of callback function.
    • timeout <number> the number of milliseconds a request can take before automatically being terminated
    • withCredentials <Boolean> default: false, whether or not cross-site Access-Control requests should be made using credentials such as cookies, authorization headers or TLS client certificates
    • autoAbort <Boolean> default: false, Whether to abort requests where from the previousroute page when the page url changed and does not refresh(For single page application).

ajax.send(data)

send the ajax request, if browser support Promise, it will return Promise.If not or jsonp, it will return itself.

  • data <string>|<object> if the request method is 'get', it's appended to query string of the URL, or it's sended to remote of body.

ajax.abort()

abort the ajax request

ajax.then(cb)

when browser can't support Promise,ajax.send() will return the ajax object, so you can use ajax.then() to get response.But if type is 'jsonp',ajax.then() will don't work.

  • cb <function> the function have one argument res object include header, status, data.

ajax.on(event, cb)

add event listener for the ajax object.if async is false, ajax.on() must be called before send,else it will not work.

  • cb <funtction> the cb is called when event happen
  • event <string>
    • success cb is fired when getting the response successfully
    • fail cb is fired when failing to get the response(eg:http status: 404, 400, 500..., or jsonp fail)
    • abort cb is fired when request is aborted
    • timeout cb is fired when request timeout
    • start cb is fired when start send the request
    • end cb is fired when request is finished whether it's a success or a failure

ajax.config(options)

specify global options, each ajax object will apply the options and own options.

  • poolSize <number> xhr pool's size

How to use in IE8-9

Please use version 1. X and 0. X.