2016-04-22 169 views
2

我正在开发一个科尔多瓦混合应用程序(目前为Android)和使用InAppBrowswer插件。发送请求在科尔多瓦InAppBrowser

我想打开一个网页,并使用发布请求在那里登录(因为它目前通过Java在实际的Android应用程序中完成)。

不知何故,这不工作,但我不明白为什么。这是我正在使用的代码:

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}); 

正如你所见,我也console.log我的“postAsString”。如果我复制这个字符串并在www.thesite.dummy的控制台中执行它,它实际上可行,但不适用于InAppBrwoser

+0

貌似https://stackoverflow.com/questions/37526321/cordova-inappbrowser-post-form-to-url的副本对我来说 – Jannis

+0

将更有意义的称为新的重复,但我同意。问题是相同的 – Sergej

回答

1

只是为了帮助任何通过Google找到此问题的人,解决方案是described at that answer,正如问题中所述注释。

的代码,改变值对于特定上下文是下面:

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" 
);