【问题标题】:i cannot open external url in webview in ionic android app我无法在 ionic android 应用程序的 webview 中打开外部 url
【发布时间】:2026-02-24 08:50:40
【问题描述】:

我有这个离子应用程序,可以在 web 视图中打开一个外部 url。我一直在ios模拟器上测试,它工作正常,现在在android模拟器上测试,但它不起作用。并且 android 监视器给了我以下错误调试消息。

08-11 19:28:52.229: D/cr_Ime(2813): [InputMethodManagerWrapper.java:56] isActive: true
08-11 19:28:52.229: D/cr_Ime(2813): [InputMethodManagerWrapper.java:65] hideSoftInputFromWindow

以及下面的错误信息

08-11 19:28:52.070: E/chromium(2813): [ERROR:gles2_cmd_decoder.cc(2109)] [.CommandBufferContext.RenderWorker-0xf43e1700]GL ERROR :GL_INVALID_OPERATION : GLES2DecoderImpl::DoBindTexImage2DCHROMIUM: <- error from previous GL command
08-11 19:28:52.096: E/chromium(2813): [ERROR:gles2_cmd_decoder.cc(2109)] [.CommandBufferContext.RenderWorker-0xf43e1700]GL ERROR :GL_INVALID_OPERATION : GLES2DecoderImpl::DoBindTexImage2DCHROMIUM: <- error from previous GL command
08-11 19:28:52.122: E/chromium(2813): [ERROR:gles2_cmd_decoder.cc(2109)] [.CommandBufferContext.RenderWorker-0xf43e1700]GL ERROR :GL_INVALID_OPERATION : GLES2DecoderImpl::DoBindTexImage2DCHROMIUM: <- error from previous GL command
08-11 19:28:52.226: E/chromium(2813): [ERROR:xwalk_autofill_client.cc(121)] Not implemented reached in virtual void xwalk::XWalkAutofillClient::OnFirstUserGestureObserved()

我完全不知道问题是什么。

function createCORSRequest(method,url)
{
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr)
  {
    xhr.open(method, url, true);
  }
  else if (typeof XDomainRequest !== "undefined")
  {
    xhr = new XDomainRequest();
    xhr.open(method, url);
  }
  else
  {
    xhr.open(method, url);
  }
  return xhr;
}

var formData = new FormData();
formData.append('portal[username]',name);
formData.append('portal[password]',password);
formData.append('portal[From]','web');

var url = endPoint+ '/login';
var xhr = createCORSRequest('POST', url);

xhr.onreadystatechange = function()
{
  if (xhr.readyState === 4 && xhr.status === 200)
  {
    var  jsonObj =   JSON.parse(xhr.responseText);
    var  status  =   jsonObj.status;
    var  message =   jsonObj.message;

    if(status==='success')
    {
      var token = jsonObj.token;
      home = 'http://127.0.0.1:8888/map/index?token='+token;
      var ref = window.open(home,'_self');
      ref.addEventListener('loadstart', function(event)
      {
        if (event.url == "http://127.0.0.1:8888/map/logout")
        {
          ref.close();
        }
      });
    }
    else
    {
      alert(message);
    }
  }
};

xhr.send(formData);

【问题讨论】:

    标签: android ios angularjs webview ionic-framework


    【解决方案1】:

    您应该可以使用$cordovaInAppBrowser 插件打开一个网址。

    $cordovaInAppBrowser.open('http://example.com', '_system');
    

    我相信您还必须在 config.xml 中授予对外部 url 的权限

    <allow-navigation href="http://example.com/*"/>
    

    【讨论】:

    • 你能告诉sn-p你在哪里做这一切吗?插件是否正确安装在您的 /plugins 文件夹中?您是否能够在实际的 Android 设备上对其进行测试?
    • @radzy 我已经添加了 sn-p。好吧,它适用于 ios,所以我不确定 android 和插件的问题是什么。即使在 android 设备上我也会遇到同样的错误
    • 对不起,我不明白,我没有看到正在使用 $cordovaInAppBrowser 插件,而是您正在使用窗口对象(可能在您的浏览器上工作但在设备上不工作) .同样,无论何时使用 cordova 插件,都应该在 $ionicPlatform.ready 事件完成后完成,例如:$ionicPlatform.ready(function () { $cordovaInAppBrowser('http://example.com', '_system'); }
    • @radzy , $cordovaInAppBrowser.open(home, '_blank', 'location=no,toolbar=no');这是我尝试过的代码之一,即使这对我也不起作用
    • 是否在 $ionicPlatform.ready 被触发后调用?