Handles XHR, XDR, WS & WSS protocols
Fork me @ https://www.github.com/jas-/comm.js
async
: Force async opterational modebinary
: Force binary mode sendsurl
: If not specified the current page location is usedheaders
: A key/value object of headers to applymethod
: The method to use (post, put, delete etc)data
: The data to be processedtimeout
: Timeout value for retriesinterval
: Interval to use for retrying send upon connection termination
Here are a couple of examples
The default use case
comm(function(err, response){
if (err) throw err;
console.log(response);
});
An example sending a post request with a JSON object
comm({
method: 'post',
data: {abc: '123', xyz: 345}
}, function(err, res){
if (err) throw err;
console.log(res);
});
Here is how you can use the websocket or secure web socket protocols
comm({
data: 'ping test',
url: 'ws://echo.websocket.org'
}, function(err, response){
if (err) throw err;
console.log(response);
});
Need to use custom headers? See RFC-4229 for a complete list
comm({
headers: {
Content-Type: 'text/plain'
}
}, function(err, response){
if (err) throw err;
console.log(response);
});
To attach a form object as the payload the simpliest method would be the following example.
var formData = new FormData(document.getElementById('form-id'));
comm({
data: formData
}, function(err, response){
if (err) throw err;
console.log(response);
});
This is the least tested protocol this library supports. It will only be used
when the clients browser is internet explorer, the version is less than 10 has
access to the window.XDomainRequest
object and if the URL specified does not
match the current window.
If you wish to use this for CORS requests which it does support you must configure your web server to allow the following header params (this example is tuned to support authentication credentials while limiting access vs. using a wildcard origin such as *)
Access-Control-Allow-Origin: https://example.com
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: Cache-Control, Content-Type
Access-Control-Allow-Credentials: true