我试图在appappowser中发布值,但没有成功。 它打开浏览器两次,没有发布数据。
var options = {
email: 'test@email.com',
item_id: 1234,
reference: 1234,
item_descr: 'description',
item_quant: 1,
item_valor: 50 * 100
};
var form = document.createElement("form");
var url = "https://testurl.com";
form.setAttribute("method","post");
form.setAttribute("action",url);
for (var data in options) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", data);
hiddenField.setAttribute("value", options[data]);
form.appendChild(hiddenField);
}
document.body.appendChild(form);
var ref = cordova.InAppBrowser.open(url, '_blank', 'location=yes');
if(ref){
form.submit();
}
答案 0 :(得分:14)
以下是使用dataUrl通过InAppBrowser发布HTML表单的另一种方法:
var pageContent = '<html><head></head><body><form id="loginForm" action="YourPostURL" method="post">' +
'<input type="hidden" name="key1" value="' + YourValue1 + '">' +
'<input type="hidden" name="key" value="' + YourValue2 + '">' +
'</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"
);
答案 1 :(得分:8)
你可以这样做
var options = {
email: 'test@email.com',
item_id: 1234,
reference: 1234,
item_descr: 'description',
item_quant: 1,
item_valor: 50 * 100
};
var script = 'var form = document.createElement("form");';
script += 'var url = "https://testurl.com";';
script += 'form.method="post"';
script += 'form.setAttribute("action",url);';
for (var data in options) {
script += 'var hiddenField = document.createElement("input");';
script += 'hiddenField.setAttribute("type", "hidden");';
script += 'hiddenField.setAttribute("name","' + data +'");';
script += 'hiddenField.setAttribute("value","' + options[data] + '");';
script += 'form.appendChild(hiddenField);';
}
script += 'document.body.appendChild(form)';
var ref = cordova.InAppBrowser.open(url, '_blank', 'location=yes');
script += 'form.submit();';
然后在onappbrowser中使用loadstop事件执行脚本,如此
ref.addEventListener('loadstop', onLoadStopFunction);
onLoadStopFunction(params){
ref.executeScript({ code: script }, executeScriptCallBack);
}
function executeScriptCallBack(params) {
if (params[0] == null) {
//error message
}
}
还有很多其他方法可以做到这一点。
答案 2 :(得分:1)
您需要使用Execute Script在loadstop或load start事件上填写动态feild值。
首先在打开链接时绑定事件:
{
var url= 'yourURL';
if( device.platform === "Android"){
ref =cordova.InAppBrowser.open(url, "_blank",'location=no,clearcache=yes,hardwareback=no,zoom=no');
}else{
ref =cordova.InAppBrowser.open(url, "_blank",'location=no,clearcache=yes,closebuttoncaption=Go back to App,toolbar=yes,presentationstyle=formsheet');
}
ref.addEventListener('loadstart',onBrowserLoadStart);
ref.addEventListener('loadstop',onBrowserLoadStop);
ref.addEventListener('loaderror', onBrowserError);
ref.addEventListener('exit', onBrowserClose);
}
然后在onBrowserLoadStop上,检查它是否是发布表格的正确页面:
function onBrowserLoadStop(event){
var cUrl= 'myformURL';
if(event.url===cUrl){
var msg;
var newHtml=YourFormHTML;
var withoutScriptHtml = $(newHtml.bold());
withoutScriptHtml.find('script').remove();
msg= " var formDiv = document.createElement('div'); formDiv.id='someFormDiv'; ";
msg+= " formDiv.innerHTML='" + withoutScriptHtml.html()+ "';" ;
msg += " document.getElementById('outerDiv').appendChild(formDiv);"; //outerDiv should be on the html page residing at cUrl
msg += " document.getElementById('yourFormName').submit();";
//console.log("the message: "+ msg);
ref.executeScript(
{
code: msg
},
function(values){
console.log(JSON.stringify(values));
}
);
}
}
答案 3 :(得分:-1)
从1页开始:
window.localStorage.setItem('tempForm', JSON.stringify(form));
location.replace('page2.html');
2页:
var passedForm = $.parseJSON(window.localStorage.getItem('tempForm'));
您可以对所有对象执行此操作。