XHR.js is a simple and lightweight AJAX request library. It integrates with Promise.js to allow you handling asynchronous requests as promises.
To start using XHR.js just include the file with a traditionnal <script>
tag without forgetting to load Promise.js first and you're good to go !
<script type="text/javascript" src="Promise.js"></script>
<script type="text/javascript" src="XHR.js"></script>
You can then make a request by simply using the XHR
function like this :
var promise = XHR({
method: 'GET',
url: 'http://www.example.com/'
});
The XHR
function takes an object of parameters as described in the Request params section.
Note that XHR
returns a Promise.js promise, see https://github.com/i20/promise for more information about dealing with promises.
XHR.defaultParams = { ... }
is the default XHR
configuration to use, it will be overloaded by any configuration set as argument of the XHR
call. Available settings are :
url
: (mandatory) The URL to call via AJAXmethod
: (optionnal, defaults to'GET'
) HTTP verb to use (GET, POST, PUT, DELETE, ...)user
: (optionnal) Username when making requests with credentialspassword
: (optionnal) Password when making requests with credentialsresponseType
: (optionnal, defaults to'auto'
) Type of response expected from the server, can be one of :'auto'
: Response type will be determined automatically by inspecting theContent-Type
response header and result in one of the following'text'
: Response will not be transformed'json'
: Response will be parsed as a JSON string and the JSON object will be passed down the chain'xml'
: Response will be parsed as a DOM string and the DOM object will be passed down the chain
async
: (optionnal, defaults totrue
) Whether the call must be done asynchronously or not. There's really a few chances that you need to set it tofalse
but if so, you can do it. Remember that synchronous requests are blocking and that the browser will freeze until the server has responded.contentType
: (optionnal, defaults to'application/x-www-form-urlencoded; charset=UTF-8'
)Content-Type
request header sent to the serverheaders
: (optionnal) Additionnal request headers to send to the server in the form{ '<Header-Name>': 'header-value' }
. Notice that settingContent-Type
here preventscontentType
from being applied.data
: (optionnal) Data to send to the server when doing non GET requests.cache
: (optionnal, defaults totrue
) Set tofalse
if cache must be bypassed. It works by adding a random parameter to the URL.