add request header on backbone

后端 未结 10 2156
伪装坚强ぢ
伪装坚强ぢ 2020-12-04 08:46

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

10条回答
  •  广开言路
    2020-12-04 09:42

    Backbone.$.ajaxSetup({
        headers: {'Authorization' :'Basic USERNAME:PASSWORD'}
    });
    

    This code set headers to Backbone ajax, so they will be sent with every Backbone.sync. You will be able to send headers without using xhr.setRequestHeader with every sync call.

    So you don't need to do the following every time:

    MyCollection.fetch({ headers: {'Authorization' :'Basic USERNAME:PASSWORD'} } );
    

    You can just do

    MyCollection.fetch();
    

    Maybe it's kind of hack but it works perfectly for my system.

提交回复
热议问题