最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Set custom header in Backbone.js not working - Stack Overflow

programmeradmin2浏览0评论

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?

Share Improve this question asked Apr 18, 2013 at 14:29 codercoder 10.5k17 gold badges73 silver badges129 bronze badges 2
  • 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
Add a ment  | 

1 Answer 1

Reset to default 11

You 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.

发布评论

评论列表(0)

  1. 暂无评论