离子3中的CORS错误 [英] CORS error in Ionic 3

查看:231
本文介绍了离子3中的CORS错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临一个非常奇怪的问题,在iOS中使用Ionic 3时,我正在尝试连接到另一个域[ddd.com],而且由于Ionic 3使用WebKit,它会将主机名更改为localhost:8080 in iOS 。

I am facing a very peculiar issue, when using Ionic 3 in iOS, I am trying to connect to a different domain [ddd.com], and since Ionic 3 uses WebKit, it changes the hostname to localhost:8080 in iOS.

这造成了一个CORS问题,我可以通过添加Allow-Control-Allow-Origin来修复它:http // localhost:8080和Access-Control-Allow-凭据:是的。

This created a CORS issue, I was able to fix it by adding Allow-Control-Allow-Origin: http//localhost:8080 and Access-Control-Allow-Credentials: true.

虽然这似乎有用,但它有一个问题,它永远不会第一次工作,即第一次下载和打开应用程序时。但是当应用程序被杀死并重新打开时它会起作用。

Though this seems to work it had one issue, it never works the "first time" i.e when the app is downloaded and opened for the very first time. But it works when the app is killed and re-opened.

任何解开这个令人讨厌的神秘感的线索都会非常感激。

Any clue to un-wrap this tangly dangly mystery would be much appreciated.


  • Krishna Sagar

推荐答案

Ionic3使用WKWebview强制执行A​​jax请求的安全性。有两种方法可以纠正。

Ionic3 use WKWebview where it enforces security for Ajax request. In 2 ways it can be rectified.

第一种解决方案:使用UIWebview而不是WKWebView。但是这些解决方案会降低App的性能。

First solution: Use UIWebview instead of WKWebView. But these solution costs the performance of the App.

第二个解决方案使用本机HTTP调用而不是角度HTTP调用。使用此插件可以轻松实现。

Second Solution Instead of angular HTTP call, Use native HTTP call. Use this plugin for easy implementation.

https://github.com/sneas/ionic-native-http-connection-backend

这篇关于离子3中的CORS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆