My server has a manual authorization. I need to put the username/password of my server to my backbone request inorder for it to go through. How may i do this? Any ideas? Tha
My approach to something like this would be overwrite the sync method in order to add the header before doing the request.
In the example you could see that I'm creating a Backbone.AuthenticatedModel
, which extends from Backbone.Model
.
This will impact all methods (GET, POST, DELETE, etc)
Backbone.AuthenticatedModel = Backbone.Model.extend({
sync: function(method, collection, options){
options = options || {};
options.beforeSend = function (xhr) {
var user = "myusername";// your actual username
var pass = "mypassword";// your actual password
var token = user.concat(":", pass);
xhr.setRequestHeader('Authorization', ("Basic ".concat(btoa(token))));
};
return Backbone.Model.prototype.sync.apply(this, arguments);
}
});
Then you have to simple extend the model you need to have authentication, from the Backbone.AuthenticatedModel
you have created:
var Process = Backbone.AuthenticatedModel.extend({
url: '/api/process',
});