aja.js

Ajax without XML

Asynchronous JavaScript And JSON(P)

Install

If you need to target browsers that don't support ES5 and XHR you will need a polyfill, for instance : https://cdn.polyfill.io/v1/polyfill.min.js

Samples

Request json data

aja()
  .url('/api/data.json')
  .on('success', function(data){
      //data is a javascript object
  })
  .go();

Load html into an element

aja()
  .url('/views/page.html')
  .into('.container')
  .go();

More options using the fluent api, terrific rest client.

aja()
  .method('get')
  .url('/api/customer')
  .data({firstname: 'john romuald'})
  .on('200', function(response){
      //well done
  })
  .go();

aja()
  .method('put')
  .url('/api/customer')
  .cache(false)
  .body({id : 12, firstname: 'john romuald', job : 'linguist'})
  .on('200', function(response){
      //well done
  })
   .on('40x', function(response){
      //something is definitely wrong
      // 'x' means any number (404, 400, etc. will match)
  })
  .on('500', function(response){
      //oh crap
  })
  .go();

Jsonp

aja()
  .url('http://otherdomain.com/api/remotescript')
  .type('jsonp')
  .jsonPaddingName('callbackParameter')
  .jsonPadding('someGlobalFunction')
  .on('success', function(data){
      //fuk cross origin policy
  })
  .go();

Raw script loading

aja()
        .url('http://platform.twitter.com/widgets.js')
        .type('script')
        .on('success', function(){
            window.twttr.widgets.load();
  })
  .go();

API

the full and detailed api generated from sources is available in jsdoc.

using aja()

the function aja() is the main entry point. call it each time you want to perform a request. aja() returns an object (the request context). then attach properties to the context using the chainable setter. once the context is configured correctly, you can start the request by calling the method go().

oh wait... to get feedback from your request, you can listen events on your context, using the usual on/off/trigger pattern.

aja()    //creates your context
    .url('/api/data.json') //attach the url propery on the context
    .on('success', cb)    //listen for feedback
    .go();                //trigger the request