Last active
August 29, 2015 14:01
-
-
Save laser/3666a3da790f36c35c1f to your computer and use it in GitHub Desktop.
Custom Header (Authentication) - JavaScript (browser)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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