Javascript CORS + Cordova:问题:Access-Control-Allow-Origin
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37398695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CORS + Cordova : issues with : Access-Control-Allow-Origin
提问by Navet
I have been searching hours on this issue, but I still can't find any solution to this.
我已经在这个问题上搜索了几个小时,但我仍然找不到任何解决方案。
I am developping an App cordova (basicely HTML / JS) So : the app runs on mobile from the navigator, and I have trouble making an ajax request to an API : https://developer.riotgames.com/But let's say that I just want to get the google page.
我正在开发一个应用程序cordova(基本上是HTML / JS)所以:该应用程序从导航器在移动设备上运行,并且我无法向API发出ajax请求:https: //developer.riotgames.com/但是假设我只想获取谷歌页面。
How on earth do I do that, is this even possible ? Here is a simple exemple :
我到底该怎么做,这甚至可能吗?这是一个简单的例子:
$.ajax({
type: "GET",
url: "https://google.com",
dataType: "text",
success: function(response){
alert("!!!");
},
error: function(error){
alert("...");
}
});
I am getting the same error again and again :
我一次又一次地收到同样的错误:
XMLHttpRequest cannot load https://google.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access
XMLHttpRequest 无法加载https://google.com/。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问原点 'null'
The origin 'null' is because I run the code from : file:///D:/Projets/LoL/www/index.html
and I read that the navigator is blocking, but it doesn't work as well if I disable the security with --disable-web-security
And of course, I don't have access to the server I want to join.
起源“null”是因为我从以下位置运行代码:file:///D:/Projets/LoL/www/index.html
并且我读到导航器正在阻塞,但是如果我禁用安全性,它就无法正常工作--disable-web-security
,当然,我无权访问我的服务器想加入。
Thanks a lot for your help !
非常感谢你的帮助 !
回答by Niels Steenbeek
You need the Cordova whitelist plugin: https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-whitelist/.
您需要 Cordova 白名单插件:https: //cordova.apache.org/docs/en/latest/reference/cordova-plugin-whitelist/。
Have this in config.xml:
在 config.xml 中有这个:
<access origin="*" />
<allow-navigation href="*"/>
And have the Content-Security-Policy meta in index.html. Something like:
并且在 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:">
回答by Deepak Bhatta
I have added following in nodejs server which solves my issue;
我在 nodejs 服务器中添加了以下内容来解决我的问题;
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
This may be helpful if you are using nodejs.
如果您使用 nodejs,这可能会有所帮助。
Thanks
谢谢
回答by Rafael M.
If you just experienced the issue starting Aug 1 2019. This Access-Control-Allow-Origin Error..(using cordova)might be related to the problem.
如果您从 2019 年 8 月 1 日开始遇到此问题。此Access-Control-Allow-Origin 错误..(使用cordova)可能与该问题有关。
回答by Rajaram Joshi
There is no need to do such thing
没有必要做这样的事情
You just try to change the permission
您只需尝试更改权限
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:usesCleartextTraffic="true"
...>
...
</application>