我正在开发一个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:">