如何提交带有标题的HTML表单 [英] How to submit a HTML form with header

查看:66
本文介绍了如何提交带有标题的HTML表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在发布请求期间提交包含标头的表单,这可能吗?我尝试使用下面的代码,但是无法发送带有已定义标头的请求.

I want to submit a form including a header during posting the request , is that possible ? I tried below code but I was unable to send the request with the defined header .


<html>
  <body>
  <script>
    var xhr = new XMLHttpRequest;
    xhr.setRequestHeader("Myheader" , "value")
  </script>
    <form action="https://example.com/myprofile/myedit" method="POST">
      <input type="hidden" name="firstname" value="Jacob&#32;" />
      <input type="hidden" name="email" value="jacob&#64;gmail&#46;com" />
      <input type="submit" value="Submit request" />
    </form>
  </body>
</html>

推荐答案

我想在发布请求期间提交包含标头的表单,这可能吗?

I want to submit a form including a header during posting the request , is that possible ?

否.

仅当您使用XMLHttpRequest或fetch进行请求时,才可以将自定义HTTP标头添加到请求中(因此,如果您使用表单提交来进行请求,则不能这样做).

You can only add custom HTTP headers to a request if you are using XMLHttpRequest or fetch to make the request (so you can't if you are using a form submission to make the request).

您可以使用 submit 事件来拦截表单提交,并使用XMLHttpRequest或fetch来发出请求………但是您也需要使用JS处理响应.

You could use the submit event to intercept the form submission and make the request with XMLHttpRequest or fetch instead … but then you'd need to handle the response with JS too.

这篇关于如何提交带有标题的HTML表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆