解决cordova跳转本机自带浏览器的问题

原文链接cordova启动首页,访问远程服务器web_app
幸福就是 你遇到一个问题千方百计找不到答案快疯了的时候,突然看到了希望。

由于已经在微信浏览器(或者手机浏览器)中开发好了web应用,现在通过app打开应用访问服务页面

第一种尝试 —— 将web应用的代码一起打包到web应用中,页面全部从app本地加载代码,但是,web请求的数据是远程,因此跨域了

第二种尝试 —— 本地webview直接访问远程地址,就类似于浏览器访问远程一样,这样就不存在跨域,代码如下:

Html代码

<!DOCTYPE html>  
<html>  
    <head>  

        <meta name="format-detection" content="telephone=no">  
        <meta name="msapplication-tap-highlight" content="no">  
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">  
        <title>Hello World</title>  
    </head>  
    <body>  
        <script type="text/javascript" src="cordova.js"></script>  
        <script type="text/javascript" src="js/index.js"></script>  
    </body>  
    <script>  
            window.location.href="http://www.sqhzg.cn/house/index.html#/index";  
    </script>  
</html> 

备注:

默认index.html页面中

Html代码

<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">  

应该删除掉,否则页面无法正常跳转。

修改config.xml配置文件,允许webview访问页面,否则app会拉动本地的浏览器显示内容。

Config.xml代码

<?xml version='1.0' encoding='utf-8'?>  
<widget id="com.jianan.wuye" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">  
    <preference name="loglevel" value="DEBUG" />  
    <feature name="Whitelist">  
        <param name="android-package" value="org.apache.cordova.whitelist.WhitelistPlugin" />  
        <param name="onload" value="true" />  
    </feature>  
    <allow-intent href="market:*" />  
    <name>wuye</name>  
    <description>  
        A sample Apache Cordova application that responds to the deviceready event.  
    </description>  
    <author email="dev@cordova.apache.org" href="http://cordova.io">  
        Apache Cordova Team  
    </author>  
    <content src="index.html" />  
    <access origin="*" />  
    <allow-intent href="http://*/*" />  
    <allow-intent href="https://*/*" />  
    <allow-intent href="tel:*" />  
    <allow-intent href="sms:*" />  
    <allow-intent href="mailto:*" />  
    <allow-intent href="geo:*" />  

 <allow-navigation href="http://*/*" />  
 <allow-navigation href="https://*/*" />  
 <allow-navigation href="data:*" />  

 <access origin="*" />  
 <access origin="http://*/*" />  
 <access origin="https://*/*" />  

</widget>  

这里写图片描述
这里写图片描述
转自“黄彪的学习笔记”,感谢!

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值