Put a custom http header in backbone

孤人 提交于 2019-12-03 03:16:05

Tallmaris answer should fix it for you though I would recommend usign jQuery ajaxSetup method to setup the headers as default values for all ajax requests as I believe you need them all the time anyway right?

Somewhere where you launch the App put in

$.ajaxSetup({
    headers: {
        'user_id':dataWeb.get("id"),
        'api_key':dataWeb.get("api_key")
    }
});

Thanks to that you'll save yourself a lot of repeated code :) keep it DRY!

(obviously you'd need to ensure that dataWeb is available in the scope of where you launch the app :) )

It seems you are passing two parameters to destroy, pass only one containing the headers and the other options together, unless the brackets order is a typo. Try this:

removeNote.destroy({ 
    headers: {
        'user_id':dataWeb.get("id"),
        'api_key':dataWeb.get("api_key")
    }, // there was an extra close-open curly here...
    async:false,
    error: function(model, response){
        console.log("KO_REMOVE_NOTE");
        console.log(response);
    },
    success : function(model, response){
        console.log("OK_REMOVE_NOTE");
        console.log(response);
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!