1

I am using the following function in my http to redirect to some url with the POST data information. I need to append the uuid1 with the new url and redirect the user to the new page http://localhost:8080/my_new_page.html with the uuid information.

I am able to see the response code 200 for the POST data but it is not being redirected, and when data is large, is unable to load the style sheets in my new url.

I am not able to find the reason for non-redirection of my url. Kindly help.

<input type="button" value="Click here to go back" onclick=postdata1(uuid1,respData);>  

function postdata1(uuid1,respData) {
  var iframe = document.createElement("iframe");
  var uniqueString = "respData";
  document.body.appendChild(iframe);
  iframe.style.display = "none";
  iframe.contentWindow.name = uniqueString;

  var form = document.createElement("form");
  form.target = uniqueString;
  form.action = 'http://localhost:8080/my_new_page.html?uuid='+uuid1;
  form.method = "POST";

  var input = document.createElement("input");
  input.type = "hidden";
  input.name = "RespData";
  input.value = respData;
  form.appendChild(input);

  document.body.appendChild(form);
  form.submit();
}
3
  • uuid1 and respData are declared anywhere in your code? Commented Feb 14, 2017 at 7:55
  • yes. they are. I tried getting their output on console too. It is returning them there. Commented Feb 14, 2017 at 9:25
  • Does this answer your question? JavaScript post request like a form submit Commented Mar 15, 2023 at 6:36

1 Answer 1

3

you have to use 2 functions one for Building HTML and another for onclick below is the code snippet. This should help you.

<html>
<body>
<input type="button" value="Click here to go back" onclick=postdata1("Heell0o","Heell");>  


</body>
<script>
function loadForm(){

  var form = document.createElement("form");
  //form.target = uniqueString;
    form.name = "RespDataForm";
  form.method = "POST";

  var input = document.createElement("input");
  input.type = "hidden";
  input.name = "RespDataInput";

  form.appendChild(input);

  document.body.appendChild(form);
}

function postdata1(uuid1,respData) {
console.log(uuid1+respData);
    var form1 = document.getElementsByName("RespDataForm")[0];
    console.log(form1.name);
      var input1 = document.getElementsByName("RespDataInput")[0];
  input1.value = respData;
  form1.action = 'http://www.google.com?uuid='+uuid1;
  form1.submit();
    return false;
}
loadForm();
</script>
</html>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.