How can I add a custom HTTP header to ajax request with js or jQuery?

前端 未结 9 1140
独厮守ぢ
独厮守ぢ 2020-11-22 03:17

Does anyone know how to add or create a custom HTTP header using JavaScript or jQuery?

9条回答
  •  鱼传尺愫
    2020-11-22 03:56

    You should avoid the usage of $.ajaxSetup() as described in the docs. Use the following instead:

    $(document).ajaxSend(function(event, jqXHR, ajaxOptions) {
        jqXHR.setRequestHeader('my-custom-header', 'my-value');
    });
    

提交回复
热议问题