【问题标题】:Send Post Request over Cordova InAppBrowser通过 Cordova InAppBrowser 发送发布请求
【发布时间】:2016-04-22 14:25:28
【问题描述】:

我正在开发一个 Cordova 混合应用程序(目前适用于 android)并使用 InAppBrowswer-Plugin。

我想打开一个网页并使用 post 请求登录(目前在实际的 Android 应用程序中是通过 Java 完成的)。

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

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 中执行

【问题讨论】:

标签: javascript cordova post phonegap-plugins inappbrowser


【解决方案1】:

只是为了帮助任何通过谷歌找到这个问题的人,解决方案是described at that answer,如问题 cmets 中所述。

代码,更改特定上下文的值如下:

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

【讨论】:

    猜你喜欢
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 2019-11-11
    相关资源
    最近更新 更多