×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Arturo Bardelli
Added: May 13, 2016 3:24 PM
Views: 22
Useful for posting data to a new page sending in data in json
  1. // Arguments :
  2. //  verb : 'GET'|'POST'
  3. //  target : an optional opening target (a name, or "_blank"), defaults to "_self"
  4. postData = function(verb, url, data, target) {
  5.   var form = document.createElement("form");
  6.   form.action = url;
  7.   form.method = verb;
  8.   form.target = target || "_self";
  9.   if (data) {
  10.     for (var key in data) {
  11.       var input = document.createElement("textarea");
  12.       input.name = key;
  13.       input.value = typeof data[key] === "object" ? JSON.stringify(data[key]) : data[key];
  14.       form.appendChild(input);
  15.     }
  16.   }
  17.   form.style.display = 'none';
  18.   document.body.appendChild(form);
  19.   form.submit();
  20. };
  21.  
  22. //USE CASE
  23. clickPost = function (){
  24. var data = { MyKeyName : "KeyValue" , MySecondKey: "Other data..." };
  25. postData("POST","http://my-hostname/my-page.aspx",data,"_blank");
  26. }