qwest 1.4.0
Qwest is a simple ajax library based on promises
behaviour and that supports XmlHttpRequest2
special data like ArrayBuffer
, Blob
and FormData
.
Install
You can pick the minified library or install it with :
jam install qwest
bower install qwest
npm install qwest --save-dev
Changes in 1.4
The response type option is now set to auto
by default.
auto
mode is only supported for xml
, json
and text
response types; for arraybuffer
, blob
and document
you'll need to define explicitly the responseType
option- if the response of your requests doesn't return a valid (and recognized)
Content-Type
header, then you must explicitly set the responseType
option
Quick examples
qwest.get('example.com')
.then(function(response){
alert(response);
});
qwest.post('example.com',{
firstname: 'Pedro',
lastname: 'Sanchez',
age: 30
})
.then(function(response){
})
.catch(function(message){
});
Basics
qwest.<method>(<url>,[data],[options])
.then(function(response){
})
.catch(function(message){
})
.complete(function(){
});
The method is either get
, post
, put
or delete
. The data
parameter can be a multi-dimensional array or object, a string, an ArrayBuffer, a Blob, etc... If you don't want to pass any data but specify some options, set data to null
.
The available options
are :
- dataType :
post
(by default), json
, text
, arraybuffer
, blob
, document
or formdata
(you don't need to specify XHR2 types since they're automatically detected) - responseType : the response type; either
auto
(default), json
, xml
, text
, arraybuffer
, blob
or document
- cache : browser caching; default is
false
for GET requests and true
for POST requests - async :
true
(default) or false
; used to make asynchronous or synchronous requests - user : the user to access to the URL, if needed
- password : the password to access to the URL, if needed
- headers : javascript object containing headers to be sent
- withCredentials :
false
by default; sends credentials with your XHR2 request (more info in that post) - timeout : the timeout for the request in ms;
3000
by default - retries : the number of times the request would be runned if the timeout is reached; 3 by default; if you want to remove the limit set it to
null
In each callback, the this
keyword refers to the XmlHttpRequest
object, so you can do some specific tasks you may need.
qwest.get('example.com')
.then(function(response){
})
.catch(function(message){
log(this.responseText);
throw message;
});
Request limitation
One of the great qwest's functionnalities is the request limitation. It avoids browser freezes and server overloads by freeing bandwidth and memory resources when you have a whole bunch of requests to do at the same time (when you load a gallery, per example). You just need to set the request limit and when the count is reached qwest will stock all further requests and start them when a slot is free.
qwest.limit(4);
$('.foo').forEach(function(){
qwest.get(this.data('some_url_to_get'));
});
If you want to remove the limit, do qwest.limit(null)
.
Set options to XHR
If you want to apply some manual options to the XHR
object, you can use the before
promise. It must be called before any other promise. The this
keyword refers to the XHR
object itself.
qwest.before(function(){
this.uploadonprogress=function(e){
};
})
.get('example.com')
.then(function(response){
});
Handling fallbacks
XHR2 is not available on every browser, so, if needed, you can simply verify the XHR version.
if(qwest.xhr2){
}
else{
}
Receiving binary data in older browsers
Getting binary data in legacy browsers needs a trick, as we can read it on MDN. In qwest, that's how we could handle it :
qwest.before(function(){
this.overrideMimeType('text\/plain; charset=x-user-defined');
})
.get('example.com/file')
.then(function(response){
});
Compatibility notes
According to this compatibility table, IE7/8 do not support using catch
and delete
as method name because these are reserved words. If you want to support those browsers you should write :
qwest.delete('example.com')
.then(function(){})
.catch(function(){});
Like this :
qwest['delete']('example.com')
.then(function(){})
['catch'](function(){});
The CORS object shipped with IE8 and 9 is XDomainRequest
. This object does not support PUT
and DELETE
requests and XHR2 types.
XHR2 does not support arraybuffer
, blob
and document
response types in synchroneous mode.
Last notes
- if an error occurs in a
then()
callback, it will be catched by the catch()
promise - the default
Content-Type
header is application/x-www-form-urlencoded
for post
and xhr2
data types, with a POST
request - if you want to set or get raw data, set the related option to
text
- as stated on StackOverflow, XDomainRequest forbid HTTPS requests from HTTP scheme and vice versa
License
MIT license everywhere!