Skip to content
Simple Ajax: a lightweight library to make AJAX requests
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

sa - Simple AJAX

The aim of sa is to provide a lightweight library to easy make AJAX requests.

Learning by examples

Instantiate the AJAX object

var ajax = null;
try {
  ajax = new AJAX();
} catch(e) {
  // handle error (XMLHttpRequest object not supported)

Instantiate the AJAX object for a CORS request

var ajax = null;
try {
  var CORS = true;
  ajax = new AJAX(CORS);
} catch(e) {
  // handle error (XMLHttpRequest object not supported)

GET request

ajax.get('/somepage?parmeter=wat&who=yello',function(data) {
  // handle completed get request
function(statusCode) { // Handle failure

POST request'/somepage', function(data) {
  // Handle completed post request
}, function(statusCode) { // Handle failure
}, parameters);

GET Request returning JSON

ajax.getJSON('/somepage?parmeter=wat&who=yello',function(data) {
  // Handle json return object, like:
  console.log(data.field1, data.field2);
}, function(statusCode) { // Handle failure

Generic request

You can build your own request.

url: '/wow',
  type: 'post',
  dataType: 'json',
  data: {wow: 'amazing', 'param2': 1},
  success: function(json) {
  failure: function(statusCode) {
    alert("Request failed with status code: " + statusCode);

In the example above we do a POST request to /wow and we expect to obtain a JSON object in respose.

We could specify JSON or XML for the expected format of the response. Empty field means HTML.


As you can see from the examples, you can use JSON object or a literal string to pass parameters.

To specify other parameters in AJAX.request you have to follow the definition below.

//define generic ajax request parameter
  type: '',
  url: '',
  data: '',
  dataType: '',
  success: function(data){},
  failure: function(errorCode){}


  • type = get|post
  • url = whatever you want
  • data: string|JSON
  • dataType: "JSON"|"XML"|""
  • success = function(data) {}
  • failure = function(errorCode) {}


sa is licensed under the terms of MIT licence.

You can’t perform that action at this time.