We are create the input element for redirection URL and send the data on redirection URL in payload and in doing so we are able to do, so But simultaneously when we set the request header on the same redirection URL but the request header is not getting set. We are unable to do this. Please help us how to set header in Request Headers when we sent data in payload on redirection URL. We are using the below code.
app.component.html
<button (click)="redirectURL()">Redirect different URL</button>
app.component.ts
setHeader(url: any, newObj: any){
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "post", url);
xmlHttp.setRequestHeader("iv-user", 'party_id');
console.log('responseText : ' , xmlHttp.responseText);
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4) {
alert(xmlHttp.responseText);
console.log('xhr responseText : ' , xmlHttp.responseText);
var data = xmlHttp.responseText;
var jsonResponse = JSON.parse(data);
return jsonResponse;
}
}
xmlHttp.send(newObj);
return xmlHttp.responseText;
}
redirectURL() {
let form = document.createElement('form');
console.log('form : ' , form);
let actionUrl = "https://reqres.in/api/users";
form.setAttribute('method',"post");
form.setAttribute('action',actionUrl);
form.setAttribute('style',"display:none");
let a = document.createElement("input");
a.type = "text";
a.name = "party_id";
a.value = "111111111";
let b = document.createElement("input");
b.type = "text";
b.name = "party_id2";
b.value = "2222222";
let c = document.createElement("input");
c.type = "text";
c.name = "party_id3";
c.value = "33333333";
let d = document.createElement("input");
d.type = "text";
d.name = "party_id4";
d.value = "44444444";
let e = document.createElement("input");
e.type = "text";
e.name = "party_id5";
e.value = "5555555";
form.appendChild(a);
form.appendChild(b);
form.appendChild(c);
form.appendChild(d);
form.appendChild(e);
document.getElementsByTagName('body')[0].appendChild(form);
const myform = document.createElement('form');
console.log('my form : ' , form);
let newObj = {
name: 'sk',
job: 'developer'
}
this.setHeader("https://reqres.in/api/users", newObj);
form.submit();
}