简体   繁体   English

Cordova InAppBrowser将表单发布到url

[英]Cordova InAppBrowser post form to url

i'm trying to post values inappbrowser but no success. 我试图发布值inappbrowser但没有成功。 it does open browser twice and no data posted. 它确实打开浏览器两次,没有发布数据。

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

Here's another way of posting a HTML form via the InAppBrowser using a dataUrl: 这是使用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"
);

you can do it like this 你可以这样做

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();';

and then execute script in the inappbrowser using on the loadstop event like this 然后使用像这样的loadstop事件在inappbrowser中执行脚本

ref.addEventListener('loadstop', onLoadStopFunction);

onLoadStopFunction(params){
   ref.executeScript({ code: script }, executeScriptCallBack);
}
function executeScriptCallBack(params) {

    if (params[0] == null) {

        //error message
    }

}

there are many other ways to do it. 还有很多其他方法可以做到这一点。

You need to fill in the dynamic feild values on loadstop or load start event by using Execute Script. 您需要使用Execute Script在loadstop或load start事件上填充动态feild值。

First Bind the events , when you open the link: 首先在打开链接时绑定事件:

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

Then on onBrowserLoadStop, check if its the right page to Post form: 然后在onBrowserLoadStop上,检查它是否是Post表格的右页:

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


}
}

From the 1 page: 从1页开始:

window.localStorage.setItem('tempForm', JSON.stringify(form));
location.replace('page2.html');

2 page: 2页:

var passedForm = $.parseJSON(window.localStorage.getItem('tempForm'));

You can do that with all objects. 您可以对所有对象执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM