离子CORS问题

时间:2019-01-03 07:54:54

标签: angular ionic-framework cors ionic3

我正面临预期的CORS问题

  • text
  • 1. ionic serve
  • 2. ionic cordova run android

我使用

解决了3. ionic cordova build android --debug的问题
  • 1. ionic serve中的本地代理

as per this blog https://blog.ionicframework.com/handling-cors-issues-in-ionic/

ionic.config.json
  • 在我的POST调用中设置Access-Control-Allow-Origin标头,例如:

>

"proxies": [
{
  "path": "/proxy",
  "proxyUrl": "https://xx.xx.xx/"
}
]

但是使用reqOpts = { headers: new HttpHeaders({ 'Content-Type':'application/json', 'Access-Control-Allow-Origin':'*', 'Access-Control-Allow-Methods':'POST, GET, PUT, OPTIONS, DELETE, PATCH', 'Accept':'application/json', }) }; 2. ionic cordova run android仍然出现以下错误:

3. ionic cordova build android --debug

博客还提到在创建APK核心时不会出现问题。但是,如果我删除代理并使用01-02 15:49:48.815 3682-3889/com.xx.xx D/SERVER: Handling local request: http://localhost:8080/proxy/xx/login 01-02 15:49:48.859 3682-3682/com.xx.xx D/SystemWebChromeClient: http://localhost:8080/build/main.js: Line 834 : [object Object] 01-02 15:49:48.859 3682-3682/com.xx.xx I/chromium: [INFO:CONSOLE(834)] "[object Object]", source: http://localhost:8080/build/main.js (834) 01-02 15:49:48.863 3682-3682/com.xx.xx D/SystemWebChromeClient: http://localhost:8080/build/vendor.js: Line 1823 : ERROR 01-02 15:49:48.864 3682-3682/com.xx.xx I/chromium: [INFO:CONSOLE(1823)] "ERROR", source: http://localhost:8080/build/vendor.js (1823)

制作APK
3. ionic cordova build android --debug

Cors问题出现。为什么我的APK会发生这种情况?在设备上不应该使用来自file://而不是localhost://的资源,从而避免CORS问题吗?

我怎么办

  

a。修复cors错误

  

b。使代理可用于APK构建。

奇怪的是,服务器回复具有Access-Control-Allow-Origin

Access to XMLHttpRequest at 'https://xx.xx.xx/xx/login' 
from origin 'http://localhost:8080' has been blocked by CORS policy:
Response to preflight request doesn't pass access control check: 
No 'Access-Control-Allow-Origin' header is present on the requested resource. main.js:835 

离子版本:4.5.0

OS:Mac OSX Mojave

平台:Android

1 个答案:

答案 0 :(得分:0)

如果您无法更改服务器设置,则无法在客户端上执行WKWebview上的CORS问题。建议,更改为离子本机http

https://ionicframework.com/docs/v3/native/http/