Git Product home page Git Product logo

sa's Introduction

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, body) { // Handle failure
  console.log(statusCode, body);
});

POST request

ajax.post('/somepage', function(data) {
  // Handle completed post request
}, function(statusCode, body) { // Handle failure

  console.log(statusCode, body);
}, 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, body) { // Handle failure
  console.log(statusCode, body);
});

Generic request

You can build your own request.

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

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.

Parameters

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, body){}
};

With:

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

License

sa is licensed under the terms of MIT licence.

sa's People

Contributors

galeone avatar winterthediplomat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.