I am developing a cordova hybrid app (currently for android) and using the InAppBrowswer-Plugin.
I want to open a web page and login there using a post request (as it is currently done via Java in an actual Android application).
Somehow this does not work though and I cannot figure out why. This is the code I am using:
var ref = cordova.InAppBrowser.open("www.thesite.dummy", "_blank", "location=yes");
var postAsString = 'var xhttp = new XMLHttpRequest();'+
'xhttp.open("POST", "'+url+'", true);'+
'xhttp.onreadystatechange = function() {if (xhttp.readyState == 4 && xhttp.status == 200) {alert("it worked")}};'+
'xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");'+
'xhttp.send("username=dummyUser&password=dummyPW")'
console.log(postAsString);
ref.executeScript({"code":postAsString});
As you can see I also console.log my "postAsString". If I copy this string and execute it in the console of www.thesite.dummy it actually works, but not in the InAppBrwoser
Just to help anyone who find this question through Google, the solution is described at that answer as said in the question comments.
The code, changing values for the specific context is below:
var pageContent = '<html><head></head><body><form id="loginForm" action="www.thesite.dummy" method="post">' +
'<input type="hidden" name="username" value="dummyUser">' +
'<input type="hidden" name="password" value="dummyPW">' +
'</form> <script type="text/javascript">document.getElementById("loginForm").submit();</script></body></html>';
var pageContentUrl = 'data:text/html;base64,' + btoa(pageContent);
var browserRef = window.cordova.InAppBrowser.open(
pageContentUrl ,
"_blank",
"hidden=no,location=no,clearsessioncache=yes,clearcache=yes"
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With