【问题标题】:Ionic 5 angular [CORS POLICY in get methode]离子 5 角度 [获取方法中的 CORS 策略]
【发布时间】:2022-01-28 12:22:16
【问题描述】:

我想从外部 api 获取数据,但由于 cors 政策而被阻止,我无法在服务器端修改标头。

我找到了一些解决方案,但对我不起作用,其中一个是使用 chrome 扩展,但是当我在我的设备上运行应用程序时,我遇到了同样的错误

错误信息:

从源“http://localhost:8100”访问“URL”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应不 通过访问控制检查:没有“Access-Control-Allow-Origin”标头 出现在请求的资源上。

这是我的代码:

const url = 'http://host.com:port_number/get.php';
let headers=new HttpHeaders({
  'Access-Control-Allow-Headers': 'Accept,X-Custom-Header',
  'Access-Control-Allow-Origin':'*',
  'Access-Control-Allow-Credentials':'true'
});
return this.http.get(url, {headers:headers, params: new HttpParams().set('username', this.log).set('password', this.pass), responseType: 'text' ,observe: 'response'}).subscribe(
  data => {
    
    console.log(data);
  },
  error => console.error(error),
  () => {
  });

【问题讨论】:

  • 所以您想绕过无法从客户端控制的服务器的 CORS 策略?
  • 是的,没错,所有者想要更新他的应用程序,它是用 Java 开发的,他们没有这个错误,但现在我想用 ionic 创建一个,但我卡住了

标签: ionic-framework ionic5


【解决方案1】:

resources > android > xml 下创建文件network_security_config.xml 并添加您要从中获取数据的域。

然后添加<application android:networkSecurityConfig="@xml/network_security_config" />

到您的 config.xml 文件

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-02-10
  • 2021-04-27
  • 1970-01-01
  • 2020-06-14
  • 1970-01-01
  • 1970-01-01
  • 2020-07-29
  • 2022-06-12
相关资源
最近更新 更多