[英]CORS issues when emulating ionic app on iOS
I recently updated the ionic-cli
and I am now trying to emulate an ionic app with the command ionic cordova emulate ios
.我最近更新了
ionic-cli
,现在我正在尝试使用命令ionic cordova emulate ios
模拟一个离子应用程序。
However, I am getting CORS errors with every api requests但是,我收到每个 api 请求的 CORS 错误
Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin.
Access-Control-Allow-Origin 不允许 Origin http://localhost:8080 。
I found that the ionic docs say this about the above command我发现离子文档对上述命令有这样的说法
Like running cordova emulate directly, but also watches for changes in web assets and provides live-reload functionality with the --livereload option.
就像直接运行cordova emulate一样,但也监视网络资产的变化并通过--livereload选项提供实时重新加载功能。
It seems like that command also starts a server, which would explain why the origin is localhost
instead of file://
.该命令似乎还启动了一个服务器,这可以解释为什么源是
localhost
而不是file://
。
However, I tried emulating with cordova emulate ios
, or building the app with ionic cordova build ios
and running the output with the simulator, but I still get CORS errors.但是,我尝试使用
cordova emulate ios
进行cordova emulate ios
,或者使用ionic cordova build ios
应用程序并使用模拟器运行输出,但我仍然遇到 CORS 错误。
Emulating on Android works fine and there is a proxy for running the app in the browser.在 Android 上模拟工作正常,并且有一个代理可以在浏览器中运行应用程序。
Any idea if I am on the right track and if there would be a way to emulate from file://
?知道我是否在正确的轨道上,是否有办法从
file://
进行模拟? Would this problem persists when releasing the app to the App Store?将应用程序发布到 App Store 时,此问题是否仍然存在?
The problem with CORS on iOS actually did not come from the ionic-cli
, but from the fact that I switched to using WKWebView
from UIWebview
. iOS 上 CORS 的问题实际上不是来自
ionic-cli
,而是来自我从UIWebview
切换到使用WKWebView
的UIWebview
。
This blog post explains more about WKWebView and why it is much better than its predecessor. 这篇博文详细解释了 WKWebView 以及为什么它比它的前身好得多。
It also mentions this concerning CORS:它还提到了关于 CORS 的这一点:
UIWebview, or the older webview in iOS, never actually enforced CORS, but WKWebView does and does not provide a way to disable it.
UIWebview 或 iOS 中较旧的 webview 从未真正强制执行过 CORS,但 WKWebView 确实也没有提供禁用它的方法。 To address this, you need to implement CORS correctly and add the following entry:
要解决此问题,您需要正确实施 CORS 并添加以下条目:
Origin: http://localhost:8080
来源: http://localhost:8080
IF this is not possible (you do not own the API), a workaround can be to use the native HTTP plugin, @ionic-native/http.
如果这是不可能的(您不拥有 API),则可以使用本机 HTTP 插件 @ionic-native/http 来解决。
So I enabled CORS on my api, and everything worked as before.所以我在我的 api 上启用了 CORS,一切都像以前一样。
try creating an ionic proxy, adding this code to ionic.config.json尝试创建一个离子代理,将此代码添加到 ionic.config.json
"proxies": [{
"path": "/api",
"proxyUrl": "http://your-server.com"
}]
and then call your server simply with http.get('/api')
然后简单地使用
http.get('/api')
调用您的服务器
If it still not working, try using the cordova http plugin instead of angular http.如果它仍然不起作用,请尝试使用cordova http插件而不是angular http。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.