Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Post Request over Cordova InAppBrowser

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

like image 365
Sergej Avatar asked Apr 22 '16 14:04

Sergej


1 Answers

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"
);
like image 73
mqueirozcorreia Avatar answered Oct 09 '22 22:10

mqueirozcorreia