Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/galeone/sa
Simple Ajax: a lightweight library to make AJAX requests
https://github.com/galeone/sa
ajax ajax-request javascript json
Last synced: 4 months ago
JSON representation
Simple Ajax: a lightweight library to make AJAX requests
- Host: GitHub
- URL: https://github.com/galeone/sa
- Owner: galeone
- Created: 2014-05-15T15:26:11.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2022-09-19T08:54:06.000Z (over 2 years ago)
- Last Synced: 2024-10-04T16:46:15.733Z (4 months ago)
- Topics: ajax, ajax-request, javascript, json
- Language: JavaScript
- Homepage:
- Size: 11.7 KB
- Stars: 11
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
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
```javascript
var ajax = null;
try {
ajax = new AJAX();
} catch(e) {
// handle error (XMLHttpRequest object not supported)
}
```## Instantiate the AJAX object for a CORS request
```javascript
var ajax = null;
try {
var CORS = true;
ajax = new AJAX(CORS);
} catch(e) {
// handle error (XMLHttpRequest object not supported)
}
```## GET request
```javascript
ajax.get('/somepage?parmeter=wat&who=yello',function(data) {
// handle completed get request
},
function(statusCode, body) { // Handle failure
console.log(statusCode, body);
});
```## POST request
```javascript
ajax.post('/somepage', function(data) {
// Handle completed post request
}, function(statusCode, body) { // Handle failureconsole.log(statusCode, body);
}, parameters);
```## GET Request returning JSON
```javascript
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.
```javascript
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.
```javascript
//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.