Skip to content

Instantly share code, notes, and snippets.

@laser
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laser/3666a3da790f36c35c1f to your computer and use it in GitHub Desktop.
Save laser/3666a3da790f36c35c1f to your computer and use it in GitHub Desktop.
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