简体   繁体   English

从 Javascript 客户端设置 header 值

[英]Setting header value from Javascript Client Side

I want to set a customer header from browser similar to what mod plugin does on chrome, is there any way to set a header from javascript client side?我想从浏览器中设置一个客户 header,类似于 chrome 上的 mod 插件,有没有办法从 javascript 客户端设置 header?

Based on a condition, I want to set a header so that all subsequent browser requests will have this header value.根据一个条件,我想设置一个 header 以便所有后续浏览器请求都具有这个 header 值。

I know this from server side, but wanted to know if it is possible from client side.我从服务器端知道这一点,但想知道从客户端是否有可能。

A simple way to do this in vanilla JS is to augment the XMLHTTPRequest.prototype.send method.在 vanilla JS 中执行此操作的一个简单方法是增加XMLHTTPRequest.prototype.send方法。

You would do something like你会做类似的事情

const defaultSend = XMLHTTPRequest.prototype.send;
XMLHTTPRequest.prototype.send = function (data) {
  this.setRequestHeader('SOME-CUSTOM-HEADER', 'header-value');
  defaultSend.call(this, data);
};

While this works, it's like using a nuke when you need a hammer.虽然这行得通,但就像在需要锤子时使用核武器一样。 It pollutes ALL of the following XHRs run in the context of that particular window.它污染了在特定 window 的上下文中运行的以下所有 XHR。 While you could do domain level filtering, but I find that it's not worth the trouble.虽然您可以进行域级别过滤,但我发现这不值得麻烦。

If you are using client XHR libraries like axios , they will often provide a better and more contextual way.如果您使用axios等客户端 XHR 库,它们通常会提供更好、更符合上下文的方式。 For example, in case of axios, you could do something like例如,在 axios 的情况下,您可以执行类似的操作

const client = axios.createClient();
client.interceptors.request.use(config => {
  config.headers['SOME-CUSTOM-HEADER'] = 'header-value';
  return config;
});
client.get('/some-url', params); // this will have the custom header that you injected

EDIT: One caveat here is that this only affects XHRs, not form submissions or asset calls.编辑:这里的一个警告是,这只会影响 XHR,而不是表单提交或资产调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在服务器端设置自定义标头,并在客户端使用JavaScript检索其值 - Setting a custom header on the server-side, and retrieve its value using JavaScript on the client side 设置标签客户端的值 - Setting Value of Label Client Side 从javascript(客户端)发送值以表示(服务器端) - sending the value from javascript(client side) to express (server side) 服务器更改了HiddenField javascript值,请在客户端进行确认 - HiddenField javascript value chanded from server confirm on client side 是否可以将Rails的值返回给客户端JavaScript? - Is it possible to return value from Rails to the client-side javascript? 从客户端在 Telerik RadGrid 中设置 GridCheckBoxColumn - Setting GridCheckBoxColumn in Telerik RadGrid from the client side 如何在客户端(JavaScript)上使用服务器端(VB.NET)的返回值? - How do you use a return value from the server side (VB.NET) on the client side (JavaScript)? 客户端标头信息 - Client side header information 使用带有页眉和页脚的 Javascript(客户端)生成 PDF - Generate PDF using Javascript (Client side) with header and footer 在BIRT报告中使用标头在客户端对数据进行排序(使用JavaScript) - Use header in BIRT report to sort data at the client side (with JavaScript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM