Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace window.open(...) with a POST

I currently have some code that runs a window.open(urlWithGetParams) line. As far as I'm aware, this is going to force me to use a GET request. I would like to do this with a POST request. Is there a workaround for this?

I'm not married to window.open(), either. I'm open to just about any alternative that allows me to spawn a new window via a POST request instead of a GET.

like image 961
corsiKa Avatar asked Jul 22 '13 16:07

corsiKa


People also ask

How do I send POST request using Windows href?

Using window. location. href it's not possible to send a POST request. What you have to do is to set up a form tag with data fields in it, set the action attribute of the form to the URL and the method attribute to POST, then call the submit method on the form tag.

How do you pass objects open in Windows?

open() method has an explicit parameter for passing in properties like height, width, toolbars, and other properties : window. open(url, target, 'Your Properties Here'); Generally these properties are passed in as a comma delimited list, however if you already have an array with them, you can use the Array.


1 Answers

In fact I made a small "library" for this, open in POST a new window :

// Arguments : //  verb : 'GET'|'POST' //  target : an optional opening target (a name, or "_blank"), defaults to "_self" window.io = {     open: function(verb, url, data, target){         var form = document.createElement("form");         form.action = url;         form.method = verb;         form.target = target || "_self";         if (data) {             for (var key in data) {                 var input = document.createElement("textarea");                 input.name = key;                 input.value = typeof data[key] === "object"                     ? JSON.stringify(data[key])                     : data[key];                 form.appendChild(input);             }         }         form.style.display = 'none';         document.body.appendChild(form);         form.submit();         document.body.removeChild(form);     } }; 

Example :

io.open('POST', 'fileServer.jsp', {request: {key:"42", cols:[2, 3, 34]}}); 

To open in a new window, set the target parameter :

io.open('POST', someURL, someArgs, 'newwin'); 

or to ensure it's a new window/tab each time :

io.open('POST', someURL, someArgs, '_blank'); 
like image 80
Denys Séguret Avatar answered Oct 08 '22 12:10

Denys Séguret