Search code examples
javascripthttpcookiesheaderxmlhttprequest

Sending custom headers and cookies


Scenario : I want a form to be posted with my custom headers and cookies when i click the submit button .

I tried the below code to do , but it was failure

<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="[email protected]" />
<input type="submit" onclick="test_post()" value="Submit request" />
</form>
</body>
</html>

I am not good in js , i used the code below

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

How can i submit the form with my custom headers and cookies , it is a XMLHttpRequest.

PS : Editing cookies and headers using addons or console is not the requitement


Solution

  • It looks like your function isn't closed. Have you considered using the Axios HTTP client? https://github.com/axios/axios Many designers prefer to avoid using old-school style xhr XMLHttpRequests. There are a number of ways these can go wrong.