Create a gist now

Instantly share code, notes, and snippets.

@laser /header.js
Last active Aug 29, 2015

What would you like to do?
Custom Header (Authentication) - JavaScript (browser)
// initialize the API client
function initApp(callback) {
function transport(req, callback) {
var settings = {
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(req),
beforeSend: function(xhr) {
// set the Authorization header
xhr.setRequestHeader('Authorization', 'Basic ' + btoa('admin:admin'));
},
success: function(data) {
callback(data);
}
};
jQuery.ajax('/todos', settings);
};
var client = Barrister.httpClient(transport);
client.loadContract(function() {
var proxy = client.proxy('TodoManager');
callback(null, proxy);
});
}
// consume the API client
initApp(function(err, TodoManager) {
TodoManager.readTodos(function(err, todos) {
jQuery('#result').text(JSON.stringify(todos));
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment