npm.io
0.1.1 • Published 11 years ago

tcb-missile

Licence
MIT
Version
0.1.1
Deps
0
Vulns
0
Weekly
0

(AJAX) Missile

Tiny AJAX library that aims to be VERY similar to jQuery's $.ajax(settings) method.

Installation

npm install git://github.com/thecodebureau/missile.git

Usage

Works exactly like jQuery's $.ajax(settingsObject) function.

Missile({
  method: 'POST',
  url: '/api/objects',
  // one can also use dataType, like jQuery
  responseType: 'json',
  contentType: 'application/json',
  data: JSON.stringify({ key: 'value' }),
  success: function(response, textStatus, xhr) {
    console.log('success');
  },
  error: function(xhr, textStatus, errorThrown) {
    console.log('error');
  }
});

Set Defaults

To set defaults for any option to be used anytime it is not supplied (supports both Missile property names, or jQuery property names. See below.):

Missile.setDefaults({
  contentType: 'application/json;charset=UTF-8'
});

Compatability With & Differences to jQuery

Besides returning native XHR objects instead of jqXHR objects and dropping pre IE8 support, Missile aims to be 100% compatible with jQuery's $.ajax() function.

Renamed Options

TCB thinks some setting OPTIONS Are not named aptly considering property names of native XHR objects. Thus, the following have been renamed. To retain afore mentioned jQuery compatibility all jQuery property's are also still valid.

  • accepts -> accept
  • dataType -> responseType
  • type -> method

Implemented functionality

property implemented differences to jQuery
accepts Yes Also available as accept.
async No N/A
beforeSend No N/A
cache No N/A
complete Yes None
contents No N/A
contentType Yes None
context No N/A
converters No N/A
crossDomain No N/A
data Yes Only accept a string (must be set according to contentType) or an object if contentType is 'application/json'.
dataFilter No N/A
dataType Yes Also available as responseType. Only accepts responseTypes accepted by the native XHR object. Which is 'arraybuffer', 'blob, 'document', 'json' and 'text'.
error Yes Returns a native XHR object instead of a jqXHR object. And it returns whatever you asked for with responseType/dataType. IE, if you asked for JSON you will get an error OBJECT back, not a status text or anything crazy like that.
global No N/A
headers Yes Not sure of jQuery's default behaviour, but Missile overrites any properties that might have been set with accept[s] or similar.
ifModified No N/A
isLocal No N/A
jsonp No N/A
jsonpCallback No N/A
mimeType No N/A
password No N/A
processData Yes The processing can currently only create JSON string, not URL encoded.
scriptCharset No N/A
statusCode No N/A
success Yes It is passed a native XHR object instead of a jqXHR object.
timeout No N/A
traditional No N/A
type Yes Also available as method.
url Yes None
username No N/A
xhr Yes In the default mode, it does not create ActiveXObject objects, only XMLHttpRequest objects
xhrFields No N/A