简体   繁体   English

在服务器端设置自定义标头,并在客户端使用JavaScript检索其值

[英]Setting a custom header on the server-side, and retrieve its value using JavaScript on the client side

Is it possible to set a custom header as part of a GET response from the server-side, and retrieve its value using JavaScript/JQuery on the client side once the response is received? 是否可以在服务器端将自定义标头设置为GET响应的一部分,并在收到响应后在客户端使用JavaScript / JQuery检索其值?

If so, what is the proper way of doing this in JavaScript? 如果是这样,用JavaScript执行此操作的正确方法是什么?

As mentioned in the comments to your question, the answer is no, not directly. 如您对问题的评论中所述,答案是否定的,不是直接的。

If you really need to do this, as a workaround you can push values as hidden fields if returning HTML, part of your response if returning JSON etc. 如果您确实需要执行此操作,作为一种解决方法,如果返回HTML,则可以将值作为隐藏字段推入,如果返回JSON,则可以将响应作为推入的一部分。

If you absolutely, unavoidably need to do this, you can make a separate HEAD XMLHTTPRequest to the desired page, and load the headers from there. 如果绝对(不可避免地)需要执行此操作,则可以对所需的页面创建一个单独的HEAD XMLHTTPRequest,然后从此处加载标头。

See Http headers in Javascript? 看到Java标头中的Http标头?

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

相关问题 服务器端和客户端JavaScript - Server-side and client-side JavaScript 从 Javascript 客户端设置 header 值 - Setting header value from Javascript Client Side 如何使用 PHP 实际检索服务器端信息,客户端 JavaScript 程序使用 fetch() function 发送给它? - How do I actually retrieve information server-side using PHP that a client-side JavaScript program sent to it with the fetch() function? 用客户端访问和设置服务器端变量的方式 - The way of accessing and setting server-side variable with client-side 访问服务器端javascript变量,以便使用客户端javascript进行操作 - Access server-side javascript variable for manipulation with client side javascript 是否可以在不使用客户端Javascript的情况下使用服务器端Javascript网站? - Is a Server-side Javascript website possible without using client-side Javascript? 服务器端(python)和客户端(javascript)设计与交互 - Server-side(python) and client-side(javascript) design and interaction 在服务器端捕获所有JavaScript客户端错误 - Catch all JavaScript client-side errors on the server-side 将客户端JavaScript变量传递给服务器端php - Pass client-side javascript variable to server-side php JavaScript:客户端与服务器端验证 - JavaScript: client-side vs. server-side validation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM