代码之家  ›  专栏  ›  技术社区  ›  Joe Scotto

使用正确的收割台时,飞行前响应不成功

  •  1
  • Joe Scotto  · 技术社区  · 7 年前

    我无法让我的离子型应用程序发布到我的API。在我的API上,我设置了以下标题:

    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Headers: Content-Type");
    header("Access-Control-Allow-Methods: GET,PUT,POST,DELETE,OPTIONS");
    

    当从邮递员或实际网站发帖时,一切都按预期工作,我看到这些邮件头又回来了,但一旦我打开应用程序并发送请求,它就不再工作了。

    GET请求工作正常,只是POST请求被破坏。我使用以下功能在我的应用程序上发送帖子请求:

     /**
      * Post to the API
      * @param path    Where to go
      * @param params  What to send
      */
     private post(path, params): Promise<any> {
       return this.http
         .post(this.apiUrl + path, params)
         .toPromise()
         .then(r => r.json());
     }
    

    我在我的爱奥尼亚应用程序中得到以下错误

    Failed to load resource: Preflight response is not successful
    XMLHttpRequest cannot load https://mmcalc.com/api/calculate. Preflight response is not successful
    

    我已经为这件事忙了将近15个小时了,我不明白为什么它不起作用。

    3 回复  |  直到 7 年前
        2
  •  1
  •   Joe Scotto    7 年前

    This

    # Always set these headers.
    Header always set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
    Header always set Access-Control-Max-Age "1000"
    Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"
    
    # Added a rewrite to respond with a 200 SUCCESS on every OPTIONS request.
    RewriteEngine On
    RewriteCond %{REQUEST_METHOD} OPTIONS
    RewriteRule ^(.*)$ $1 [R=200,L]
    
        3
  •  0
  •   Christoph Kappestein    7 年前