How to submit a HTML form with header

。_饼干妹妹 提交于 2020-01-30 08:46:25

问题


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>


回答1:


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

No.

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).

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.



来源:https://stackoverflow.com/questions/57459865/how-to-submit-a-html-form-with-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!