英文:
How to set Authorization header in HTML form of method GET
问题
我想要将HTML表单提交到一个接受queryString
中的表单数据以及授权标头的服务器。然后,服务器在数据和授权标头上执行一些计算后,将HTML文档发送回来。
限制
- 我不能使用像AJAX或fetch这样的JavaScript功能,因为表单提交会获得HTML响应,需要在浏览器上打开。
- 由于内部原因,表单也需要使用
GET
方法。
响应
收到的响应是一个包含JavaScript的HTML文档,其中包含sessionStorage.setItem('data', 'value')
,它的作用范围是原始域。表单存在于parent.example.com原始域中,而接收到的HTML文档位于child.example.com原始域中。因此,我们必须像HTML表单重定向一样前往child.example.com页面。
英文:
I want to submit an HTML form to a server which accepts the form data in the queryString
along with Authorization header. The server then sends back a HTML document after performing some computations on the data and the Authorization header.
Restrictions
- I cannot use JavaScript features like AJAX or fetch since the form submission gets a HTML response which needs to be opened on the browser.
- The form also needs to be of the method
GET
for internal reasons.
Response
The response received is a HTML document which has JavaScript in it which has sessionStorage.setItem('data', 'value')
in it, which is scoped to the origin. The form exists in the parent.example.com origin, whrereas the received HTML document is in child.example.com origin. So, we Must go to the child.example.com page like a HTML form redirection.
答案1
得分: 3
以下是翻译好的部分:
-
There is no way for client-side code to do this.
- 客户端代码无法完成此操作。
-
The closest you could come would be to have the server respond to the request with a
401 Unauthorized
status and aWWW-Authenticate: Basic
header which would cause the browser to prompt the user for credentials with its native UI and, after the user enters them, repeat the request with an addedAuthorization
using the format of Basic Auth.- 最接近的方法是让服务器以
401 Unauthorized
状态和WWW-Authenticate: Basic
标头回应请求,这将导致浏览器使用其本地用户界面提示用户输入凭据,用户输入后,使用Basic Auth格式添加Authorization
并重复请求。
- 最接近的方法是让服务器以
-
As Phil mentions in the comments, it does sound like the URL you want to request simply isn't designed to be navigated to directly by a browser.
- 正如Phil在评论中提到的,听起来你想请求的URL并不是设计成可以直接在浏览器中访问的。
-
Rather than trying to work around that, you would probably be better off changing the authentication system to something else (such as cookies which are set in response to a submission from a traditional login form).
- 与其试图绕过这个问题,你可能最好改变认证系统,采用其他方式(比如在传统登录表单的响应中设置的Cookie)。
英文:
There is no way for client-side code to do this.
The closest you could come would be to have the server respond to the request with a 401 Unauthorized
status and a WWW-Authenticate: Basic
header which would cause the browser to prompt the user for credentials with its native UI and, after the user enters them, repeat the request with an added Authorization
using the format of Basic Auth.
As Phil mentions in the comments, it does sound like the URL you want to request simply isn't designed to be navigated to directly by a browser.
Rather than trying to work around that, you would probably be better off changing the authentication system to something else (such as cookies which are set in response to a submission from a traditional login form)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论