Makes writing Ajax requests with XMLHttpRequest easier
Installation
Node
npm install innosoft-ajax --save
Usage
Examples
ajax.post('/posts', {
title: 'Ajax is simple'
}).then(function(status, data){
//
});
Options
GET
, POST
, PUT
, PATCH
, DELETE
method: The HTTP method to use for the request.
ajax.get(url, data [, settings])
ajax.post(url, data [, settings])
ajax.put(url, data [, settings])
ajax.patch(url, data [, settings])
ajax.delete(url, data [,settings])
function(textStatus, data)
then: A function to be called when the request finishes. The function gets passed two arguments: The data
response and a string categorizing the status of the request (info
, success
, warning
, error
).
Settings
These are the available config options for making requests.
ajax.post(url, data, {
contentType: false, // default: true
headers: {
// custom headers to be sent
},
events: {
// progress on transfers from the server to the client (downloads)
progress: function(evt) {},
load: function(evt) {},
error: function(evt) {},
abort: function(evt) {}
},
uploadEvents: {
// progress on transfers from the client to the server (uploads)
progress: function(evt) {},
load: function(evt) {},
error: function(evt) {},
abort: function(evt) {}
}
}).then(function(status, data){
//
});
Ajax setup
Set default values for future Ajax requests. Its use is not recommended.
For example, CSRF Protection in Laravel.
<meta name="csrf-token" content="{{ csrf_token() }}">
....
....
<script type="text/javascript">
ajax.setup({
headers: {
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content')
}
});
</script>
Browser Support
Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | 9+✔ |