微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – CORS Cordova:问题:Access-Control-Allow-Origin

我一直在搜索这个问题,但我仍然找不到任何解决方法.

我正在开发一个App cordova(basicely HTML / JS)
所以:应用程序在导航器上运行,我无法向API发出ajax请求:https://developer.riotgames.com/
但是,让我们说我只是想获得谷歌页面.

我怎么做到这一点,这甚至可能吗?
这是一个简单的例子:

$.ajax({
    type: "GET",
    url: "https://google.com",
    dataType: "text",
    success: function(response){
        alert("!!!");
    },
    error: function(error){
        alert("...");
    }
});

我一次又一次地得到同样的错误

XMLHttpRequest cannot load 07001. No
‘Access-Control-Allow-Origin’ header is present on the requested
resource. Origin ‘null’ is therefore not allowed access

原点’null’是因为我从以下代码运行代码:file:/// D:/Projets/LoL/www/index.html并且我读到导航器正在阻塞,但是如果我禁用它也不起作用使用–disable-web-security的安全性
当然,我无法访问我想加入的服务器.

非常感谢你的帮助 !

解决方法:

你需要Cordova白名单插件https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-whitelist/.

在config.xml中有这个:

<access origin="*" />
<allow-navigation href="*"/>

并在index.html中使用Content-Security-Policy元.就像是:

<Meta http-equiv="Content-Security-Policy" content="default-src *; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data:">

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐