I am trying to set a custom header in my backbone.js
application in a save
method. The header isn't being set, even though I can set the header successfully in a fetch
method.
switch(method){
case 'read' :
model.fetch({
headers:{
"Override-Authorization":'Basic ' + proxyAuth
}
});
break;
case 'create' || 'update' :
sendAuthentication = function (xhr) {
xhr.setRequestHeader('Override-Authorization','Basic ' + proxyAuth);
};
model.save({
beforeSend:sendAuthentication,
headers:{
"Override-Authorization":'Basic ' + proxyAuth
}
});
break;
case 'delete' :
model.delete({
headers:{
"Override-Authorization":'Basic ' + proxyAuth
}
});
break;
}
In the first case, with fetch
call, the header gets set properly. In the second case, with the save
method, the header isn't set, even though I've tried it both ways. Any thoughts?
I am trying to set a custom header in my backbone.js
application in a save
method. The header isn't being set, even though I can set the header successfully in a fetch
method.
switch(method){
case 'read' :
model.fetch({
headers:{
"Override-Authorization":'Basic ' + proxyAuth
}
});
break;
case 'create' || 'update' :
sendAuthentication = function (xhr) {
xhr.setRequestHeader('Override-Authorization','Basic ' + proxyAuth);
};
model.save({
beforeSend:sendAuthentication,
headers:{
"Override-Authorization":'Basic ' + proxyAuth
}
});
break;
case 'delete' :
model.delete({
headers:{
"Override-Authorization":'Basic ' + proxyAuth
}
});
break;
}
In the first case, with fetch
call, the header gets set properly. In the second case, with the save
method, the header isn't set, even though I've tried it both ways. Any thoughts?
-
1
case 'create' || 'update' :
doesn't do what you think it does. – mu is too short Commented Apr 18, 2013 at 16:00 - to expound on mu's ment: developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Eric Brandel Commented Oct 21, 2013 at 19:34
1 Answer
Reset to default 11You may want to have a look at the Model#save
's doc.
model.save([attributes], [options])
You're actually setting some attributes to your model instead of overriding the headers.
model.save({}, {
beforeSend:sendAuthentication,
headers:{
"Override-Authorization":'Basic ' + proxyAuth
}
});
will do the trick.