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

前端 未结 9 1118
独厮守ぢ
独厮守ぢ 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 04:17

    You can also do this without using jQuery. Override XMLHttpRequest's send method and add the header there:

    XMLHttpRequest.prototype.realSend = XMLHttpRequest.prototype.send;
    var newSend = function(vData) {
        this.setRequestHeader('x-my-custom-header', 'some value');
        this.realSend(vData);
    };
    XMLHttpRequest.prototype.send = newSend;
    

提交回复
热议问题