JavaScript

Post Parameters in Window.open / Window.location.href (Alternative)

In some cases we have a requirement, to POST data i.e. you’re accessing the URL can’t see the parameters passed, as some information might be sensitive so we can’t pass POST parameters in URL, but there is an alternative way to post parameters.

Let’s see the Example –
The below method is an alternative to Window.open or Window.location.href, if you want to POST the parameters.

JavaScript Method:-

var openPost = function (url, params) {
    var formElement = document.createElement("form");
    formElement.setAttribute("method", "post");
    formElement.setAttribute("action", url);
    formElement.setAttribute("target", "_parent");

    for (param in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("name", param);
        hiddenField.setAttribute("value", params[param]);
        formElement.appendChild(hiddenField);
    }

    document.body.appendChild(formElement);
    formElement.submit();
}

Calling Method to Redirect -
// CALLING THE OPENPOST METHOD TO REDIRECT
var params = [];                            
params["token"] = “xyz112121212xysy”;
                               
openPost('/your/redirectURL', params);

//END OPEN POST METHOD
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.
^