【问题标题】:Sending custom headers and cookies发送自定义标头和 cookie
【发布时间】:2021-09-11 15:51:07
【问题描述】:

场景: 当我点击提交按钮时,我想要一个带有我的自定义标题和 cookie 的表单。

我尝试了下面的代码,但是失败了

<html>
<body>
<form  action="https://www.example.com/test/alter/" method="POST">
<input type="hidden" name="firstname" value="Test" />
<input type="hidden" name="email" value="test@example.com" />
<input type="submit" onclick="test_post()" value="Submit request" />
</form>
</body>
</html>

我js不好,我用了下面的代码

<script>
function test_post(){
document.getElementById("myForm").submit();
xhr.setRequestHeader('Header1','Test_header');
document.cookie="userid=123;"
</script>

如何使用我的自定义标头和 cookie 提交表单,它是一个 XMLHttpRequest。

PS : 使用插件或控制台编辑 cookie 和标头不是必要条件

【问题讨论】:

    标签: javascript http cookies header xmlhttprequest


    【解决方案1】:

    您的函数似乎没有关闭。 您是否考虑过使用Axios HTTP clienthttps://github.com/axios/axios 许多设计师更喜欢避免使用老式风格的 xhr XMLHttpRequests。有很多方法可能会出错。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-26
      • 2014-07-07
      • 2013-08-28
      • 2015-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多