Ionic CLI 6.13.0 上的 CORS 问题

CORS Issue on Ionic CLI 6.13.0

我正在尝试在 ionic 中使用后端 php 发出 http post 请求,..

这是我的离子代码:

        let body = {
            'data': ['data [0]', 'data [1]']
        };
        let options = {
            headers : { 'Content-Type' : 'application/json' }
        };
        const ifSuccess = (response) => {
            console.log(response);
            this.data = response;
        };
        const ifFail = (response) => {
            this.error = 'Oops! Une erreur au niveau du serveur...';
        };
        this.http.post('http://localhost/cantine-itu/test', body, options).subscribe(ifSuccess, ifFail);

这是后端(在 php 航班中):

Flight::before('json', function () {
    header('Access-Control-Allow-Origin: http://localhost:8100');
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Allow-Methods: GET,PUT,POST,DELETE');
    header('Access-Control-Allow-Headers: *');
});

Flight::route('POST /test', function(){
    $data = Flight::request()->data;
    Flight::json($data);
});

我收到一条错误消息:Access to XMLHttpRequest at 'http://localhost/cantine-itu/test' from origin 'http://localhost:8100' 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. (error-image)

您是从网络浏览器还是特定设备收到此错误?

由于这是本地开发而不是生产,我会设置为最宽松的可能。

  'capacitor://localhost',
  'ionic://localhost',
  'http://localhost',
  'http://localhost:8080',

https://ionicframework.com/docs/troubleshooting/cors