Last active
August 29, 2015 14:01
-
-
Save laser/f3c3bdf6d1e7956e4168 to your computer and use it in GitHub Desktop.
Custom Transport / Headers - 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