Ajax Request is an easy-to-use javascript module that sends request to the server and get response your way.
To install you'll just need:
- Bower - A package manager for the web
# Installing bower globally
$ npm install -g bower
# Getting package
$ bower install ajax-requests
Insert the file above your </body>
element:
<!doctype>
<html>
<head>
<title>Ajax Request</title>
</head>
<body>
<!-- Your content goes here -->
<script src="/path/to/script/ajax-request.min.js"></script>
</body>
<html>
Make the request:
AjaxRequest.init({
url: 'src/php/foobar.php',
method: 'POST', # Could be POST or GET
loading: foo, # Optional. ID of element that will contains the gif loading
loadImg: '<img src="src/images/loading.gif" alt="loading" />', # Optional. The GIF
data: {foo: bar} # Object with the parameters to the server file
}).then(function(res){
# If you get a json response
var result = JSON.parse(res);
try {
console.log(result); # Log the result
} catch (e) {
console.log(new Error(e));
}
}, function(err){
console.log(new Error(err));
});
url
: the file within the server [Required]method
: (GET | POST) [Required]loading
: ID of the element that will contains the processing status image [Optional]loadImg
: The GIF image [Required ifloading
parameter was defined]data
: Object containing the data that will be send to the server [Required if themethod
parameter was POST]
Feel free to send pull requests!